{"id":672,"date":"2018-05-21T00:24:10","date_gmt":"2018-05-21T00:24:10","guid":{"rendered":"http:\/\/www.afternerd.com\/blog\/?p=672"},"modified":"2019-01-13T03:22:08","modified_gmt":"2019-01-13T03:22:08","slug":"append-vs-extend","status":"publish","type":"post","link":"https:\/\/www.afternerd.com\/blog\/append-vs-extend\/","title":{"rendered":"Python Lists: Append vs Extend (With Examples)"},"content":{"rendered":"<p>Two of the most common <a href=\"https:\/\/www.afternerd.com\/blog\/python-lists-for-absolute-beginners\/\" target=\"_blank\" rel=\"noopener\">List<\/a> methods in Python are the <span class=\"symbol\">append<\/span> and <span class=\"symbol\">extend<\/span> methods.<\/p>\n<p>However, these two methods are the cause of a lot of confusion and misunderstanding among Python beginners.<\/p>\n<p>In this article, I will explain what each method does and show you exactly the difference between them.<\/p>\n<h2>First: Append<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-723\" src=\"http:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/appending.png\" alt=\"\" width=\"579\" height=\"201\" srcset=\"https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/appending.png 579w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/appending-300x104.png 300w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/appending-200x69.png 200w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/appending-400x139.png 400w\" sizes=\"(max-width: 579px) 100vw, 579px\" \/><\/p>\n<p>The <span class=\"symbol\">append<\/span> method is used to add an object to a list.<\/p>\n<p>This object can be of <strong>any data type<\/strong>, a string, an integer, a boolean, or even another list.<\/p>\n<p>Say you want to\u00a0<strong>append<\/strong>\u00a0an item to a list <span class=\"symbol\">L&gt;\/span&gt; that initially has 4 elements<\/span><\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; L = [1, 2, 3, 4]\r\n&gt;&gt;&gt; L.append(5)\r\n&gt;&gt;&gt; L\r\n[1, 2, 3, 4, 5]<\/code><\/pre>\n<p>As you can see, the append method adds the new item 5 to the list.<\/p>\n<p>Needless to say, the length of the list has increased by one (and only one) because the append method adds <strong>only one object<\/strong> to the list.<\/p>\n<p>This is an important distinction because you will see later that this is not necessarily the case with <span class=\"symbol\">extend<\/span>.<\/p>\n<p>Alright, just out of curiosity let&#8217;s try to append <strong>a list<\/strong> to our list.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; L = [1, 2, 3, 4]\r\n&gt;&gt;&gt; L.append([5, 6, 7])\r\n&gt;&gt;&gt; L\r\n[1, 2, 3, 4, [5, 6, 7]]<\/code><\/pre>\n<p>So what we did here is we appended <strong>one object<\/strong> (which happens to be of type list) to our list <span class=\"symbol\">L<\/span><\/p>\n<p>Again, after the modification the list length grew by only one.<\/p>\n<p>Now let&#8217;s take a look at a similar, yet different, method.<\/p>\n<h2>Second: Extend<\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-725\" src=\"http:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/extending.png\" alt=\"\" width=\"631\" height=\"241\" srcset=\"https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/extending.png 631w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/extending-300x115.png 300w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/extending-200x76.png 200w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/extending-400x153.png 400w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/05\/extending-600x229.png 600w\" sizes=\"(max-width: 631px) 100vw, 631px\" \/><\/p>\n<p><span class=\"symbol\">extend<\/span> is another very common list method.<\/p>\n<p>Unlike append that can take an object of <strong>any type<\/strong> as an argument, extend can only take an <a href=\"https:\/\/stackoverflow.com\/questions\/9884132\/what-exactly-are-iterator-iterable-and-iteration\" target=\"_blank\" rel=\"noopener\">iterable object<\/a> as an argument.<\/p>\n<p>An iterable object is an object that you can iterate through like strings, lists, tuples, dicts, or any object with the <span class=\"symbol\">__iter__()<\/span> method.<\/p>\n<p>What <span class=\"symbol\">extend<\/span> does is very straightforward, it iterates through the <strong>iterable<\/strong> object one item at a time and appends each item to the list.<\/p>\n<p>For example, let&#8217;s try to extend a list by another list.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; L = [1, 2, 3, 4]\r\n&gt;&gt;&gt; L.extend([5, 6, 7])\r\n&gt;&gt;&gt; L\r\n[1, 2, 3, 4, 5, 6, 7]<\/code><\/pre>\n<p>As you can see in the example above, <span class=\"symbol\">extend<\/span> takes a list (which is an iterable) as an argument and appends each item of the list to L.<\/p>\n<p>Three integer objects were appended to the list and the list size grew by three.<\/p>\n<p>This behavior is obviously different from that of the <span class=\"symbol\">append<\/span> method.<\/p>\n<p>Let&#8217;s look at another example with a different iterable object, strings.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; L = [1, 2, 3, 4]\r\n&gt;&gt;&gt; L.extend(\"hi\")\r\n&gt;&gt;&gt; L\r\n[1, 2, 3, 4, 'h', 'i']<\/code><\/pre>\n<p>Same thing!<\/p>\n<p><span class=\"symbol\">extend<\/span> iterates through the characters of &#8220;hi&#8221; and appends each character to L.<\/p>\n<h2>Side Note: How is Extend different from the (+) Operator?<\/h2>\n<p>One question you might ask is how the extend method is different from using the + operator.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; L = [1, 2, 3, 4]\r\n&gt;&gt;&gt; L + [5, 6, 7]\r\n[1, 2, 3, 4, 5, 6, 7]<\/code><\/pre>\n<p>This above code seems to have the same effect of extending a list by another list, right?<\/p>\n<p>Well there are two major differences:<\/p>\n<h3>1. you can&#8217;t use the (+) operator to extend a list by any iterable other than a list<\/h3>\n<p>For example, you can&#8217;t use the (+) operator to extend a list with a string, or a tuple, or a dict.<\/p>\n<h3>2. extend modifies the list in place whereas the (+) operator creates a new list<\/h3>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; L = [1, 2, 3, 4]\r\n&gt;&gt;&gt; L + [5, 6]\r\n[1, 2, 3, 4, 5, 6]\r\n&gt;&gt;&gt; L\r\n[1, 2, 3, 4]<\/code><\/pre>\n<p>In the previous example, notice that L has not changed at all.<\/p>\n<p>This is because the plus operator creates a new list object that holds the concatenated list.<\/p>\n<p>To use the resulting list, you might need to store it in another variable first.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; c = L + [5, 6]\r\n&gt;&gt;&gt; c\r\n[1, 2, 3, 4, 5, 6]<\/code><\/pre>\n<blockquote><p><strong>Read:<\/strong> <a href=\"https:\/\/www.afternerd.com\/blog\/difference-between-list-tuple\/\" target=\"_blank\" rel=\"noopener\">Python Lists vs Tuples<\/a><\/p><\/blockquote>\n<h2>Conclusion<\/h2>\n<p>1- Both <strong>extend<\/strong> and <strong>append<\/strong> are list methods that are used to add items to a list.<\/p>\n<p>2- <strong>append<\/strong> adds one object of any type to a list.<\/p>\n<p>3- <strong>extend<\/strong> operates on <em>iterable objects<\/em> and appends every item in the iterable to the list.<\/p>\n<p>4- using the (+) operator is not equivalent to using the <strong>extend<\/strong> method.<\/p>\n<h3>Learning Python?<\/h3>\n<p>Check out <a href=\"https:\/\/courses.afternerd.com\/\">the Courses section!<\/a><\/p>\n\n<h2>Featured Posts<\/h2>\n<ul>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-python\/\">The Python Learning Path (From Beginner to Mastery)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/learn-computer-science\/\">Learn Computer Science (From Zero to Hero)<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/coding-interview\/\">Coding Interview Preparation Guide<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/stock-investing-for-beginners\/\">The Programmer&#8217;s Guide to Stock Market Investing<\/a><\/li>\n<li><a href=\"https:\/\/www.afternerd.com\/blog\/start-programming-blog\/\">How to Start Your Programming Blog?<\/a><\/li>\n<\/ul>\n<div class=\"after-post-box\">\n<h2>Are you Beginning your Programming Career?<\/h2>\n<h3>I provide my best content for beginners in the newsletter.<\/h3>\n<ul>\n<li>Python tips for beginners, intermediate, and advanced levels.<\/li>\n<li>CS Career tips and advice.<\/li>\n<li>Special discounts on my premium courses when they launch.<\/li>\n<\/ul>\n<p>And so much more&#8230;<\/p>\n<h3>Subscribe now. It&#8217;s Free.<\/h3>\n<p><script type=\"text\/javascript\" src=\"\/\/mautic.afternerd.com\/form\/generate.js?id=2\"><\/script><\/p>\n<\/div>\n\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":731,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13],"yst_prominent_words":[410,411,518,513,415,509,508,242,504,521,517,412,514,229,467,510,512,520,519,522],"_links":{"self":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/672"}],"collection":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/comments?post=672"}],"version-history":[{"count":12,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/672\/revisions"}],"predecessor-version":[{"id":1264,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/672\/revisions\/1264"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media\/731"}],"wp:attachment":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media?parent=672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/categories?post=672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/tags?post=672"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}