{"id":110204,"date":"2020-03-08T11:06:17","date_gmt":"2020-03-08T11:06:17","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=6625"},"modified":"2020-03-08T11:06:17","modified_gmt":"2020-03-08T11:06:17","slug":"python-list-append-vs-extend","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/03\/08\/python-list-append-vs-extend\/","title":{"rendered":"Python List append() vs extend()"},"content":{"rendered":"<p>A profound understanding of Python lists is fundamental to your Python education. Today, I wondered: what&#8217;s the difference between two of the most-frequently used <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-list-methods\/\" target=\"_blank\">list methods<\/a>: <code>append()<\/code> vs. <code>extend()<\/code>?<\/p>\n<p>I shot a small video explaining the difference and which method is faster&#8212;you can play it as you read over this tutorial:<\/p>\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" title=\"Python List append() vs extend() - Semantic and Speed Difference\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/VGg8sNJ9kOM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\n<p>Here&#8217;s the short answer &#8212; <code>append()<\/code> vs <code>extend()<\/code>:<\/p>\n<ul>\n<li><strong>The method <code>list.append(x)<\/code> adds element <code>x<\/code> to the end of the <code>list<\/code>. <\/strong><\/li>\n<li><strong>The method <code>list.extend(iter)<\/code> adds all elements in <code>iter<\/code> to the end of the <code>list<\/code>.<\/strong><\/li>\n<\/ul>\n<p><strong>The difference between <code>append()<\/code> and <code>extend()<\/code> is that the former adds only one element and the latter adds a collection of elements to the list.<\/strong><\/p>\n<p>You can see this in the following example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">>>> l = []\n>>> l.append(1)\n>>> l.append(2)\n>>> l\n[1, 2]\n>>> l.extend([3, 4, 5])\n>>> l\n[1, 2, 3, 4, 5]<\/pre>\n<p>In the code, you first add integer elements 1 and 2 to the list using two calls to the <code>append()<\/code> method. <a href=\"https:\/\/blog.finxter.com\/python-list-append\/\" target=\"_blank\" rel=\"noreferrer noopener\">(If you need a deeper understanding, check out my detailed article about the append() method on this blog.)<\/a><\/p>\n<p>Then, you use the extend method to add the three elements 3, 4, and 5 in a single call of the <code>extend()<\/code> method.<\/p>\n<h3><strong>Which Method is Faster &#8212; extend() or append()?<\/strong><\/h3>\n<p>To answer this question, I&#8217;ve written a short script that tests the runtime performance of creating large lists of increasing sizes using the <code>extend()<\/code> and the <code>append()<\/code> methods. <\/p>\n<p>My thesis is that the <code>extend()<\/code> method should be faster for larger list sizes because Python can append elements to a list in a batch rather than by calling the same method again and again.<\/p>\n<p>I used my notebook with an Intel(R) Core(TM) i7-8565U 1.8GHz processor (with Turbo Boost up to 4.6 GHz) and 8 GB of RAM. <\/p>\n<p>Then, I created 100 lists with both methods, extend() and append(), with sizes ranging from 10,000 elements to 1,000,000 elements. As elements, I simply incremented integer numbers by one starting from 0.<\/p>\n<p>Here&#8217;s the code I used to measure and plot the results: which method is faster&#8212;append() or extend()?<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import time def list_by_append(n): '''Creates a list &amp; appends n elements''' lst = [] for i in range(n): lst.append(n) return lst def list_by_extend(n): '''Creates a list &amp; extends it with n elements''' lst = [] lst.extend(range(n)) return lst # Compare runtime of both methods\nlist_sizes = [i * 10000 for i in range(100)]\nappend_runtimes = []\nextend_runtimes = [] for size in list_sizes: # Get time stamps time_0 = time.time() list_by_append(size) time_1 = time.time() list_by_extend(size) time_2 = time.time() # Calculate runtimes append_runtimes.append((size, time_1 - time_0)) extend_runtimes.append((size, time_2 - time_1)) # Plot everything\nimport matplotlib.pyplot as plt\nimport numpy as np append_runtimes = np.array(append_runtimes)\nextend_runtimes = np.array(extend_runtimes) print(append_runtimes)\nprint(extend_runtimes) plt.plot(append_runtimes[:,0], append_runtimes[:,1], label='append()')\nplt.plot(extend_runtimes[:,0], extend_runtimes[:,1], label='extend()') plt.xlabel('list size')\nplt.ylabel('runtime (seconds)') plt.legend()\nplt.savefig('append_vs_extend.jpg')\nplt.show()<\/pre>\n<p>The code consists of three high-level parts:<\/p>\n<ul>\n<li>In the first part, you define two functions <code>list_by_append(n)<\/code> and <code>list_by_extend(n)<\/code> that take as input argument an integer list size <code>n<\/code> and create lists of successively increasing integer elements using the <code>append()<\/code> and <code>extend()<\/code> methods, respectively.<\/li>\n<li>In the second part, you compare the runtime of both functions using 100 different values for the list size <code>n<\/code>. <\/li>\n<li>In the third part of, you plot everything using the Python <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/matplotlib-line-plot\/\" target=\"_blank\">matplotlib library<\/a>.<\/li>\n<\/ul>\n<p>Here&#8217;s the resulting plot that compares the runtime of the two methods append() vs extend(). On the x axis, you can see the list size from 0 to 1,000,000 elements. On the y axis, you can see the runtime in seconds needed to execute the respective functions.<\/p>\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/03\/append_vs_extend-1.jpg\" alt=\"\" class=\"wp-image-6631\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/03\/append_vs_extend-1.jpg 640w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/03\/append_vs_extend-1-300x225.jpg 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure>\n<p>The resulting plot shows that both methods are extremely fast for a few tens of thousands of elements. In fact, they are so fast that the <code>time()<\/code> function of the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/2\/library\/time.html#time.time\" target=\"_blank\">time module<\/a> cannot capture the elapsed time.<\/p>\n<p>But as you increase the size of the lists to hundreds of thousands of elements, the <code>extend()<\/code> method starts to win:<\/p>\n<p><strong>For large lists with one million elements, the runtime of the <code>extend()<\/code> method is 60% faster than the runtime of the <code>append()<\/code> method.<\/strong><\/p>\n<p>The reason is the already mentioned batching of individual append operations. <\/p>\n<p>However, the effect only plays out for very large lists. For small lists, you can choose either method. Well, for clarity of your code, it would still make sense to prefer <code>extend()<\/code> over <code>append()<\/code> if you need to add a bunch of elements rather than only a single element. <\/p>\n<h2>Your Coding Skills &#8211; What&#8217;s the Next Level?<\/h2>\n<p>If you love coding and you want to do this full-time from the comfort of your own home, you&#8217;re in luck: <\/p>\n<p>I&#8217;ve created a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">free webinar<\/a> that shows you how I started as a Python freelancer after my computer science studies working from home (and seeing my kids grow up) while earning a full-time income working only part-time hours.<\/p>\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">Webinar: How to Become Six-Figure Python Freelancer?<\/a><\/p>\n<p>Join 21,419 ambitious Python coders. It&#8217;s fun! <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/1f604.png\" alt=\"\ud83d\ude04\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/1f40d.png\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A profound understanding of Python lists is fundamental to your Python education. Today, I wondered: what&#8217;s the difference between two of the most-frequently used list methods: append() vs. extend()? I shot a small video explaining the difference and which method is faster&#8212;you can play it as you read over this tutorial: Here&#8217;s the short answer [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-110204","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/110204","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=110204"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/110204\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=110204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=110204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=110204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}