{"id":570,"date":"2018-04-02T03:08:04","date_gmt":"2018-04-02T03:08:04","guid":{"rendered":"http:\/\/www.afternerd.com\/blog\/?p=570"},"modified":"2019-01-13T03:26:38","modified_gmt":"2019-01-13T03:26:38","slug":"python-copy-list","status":"publish","type":"post","link":"https:\/\/www.afternerd.com\/blog\/python-copy-list\/","title":{"rendered":"Python: How to Copy a List? (The Idiomatic Way)"},"content":{"rendered":"<p>There are many ways to copy a <a href=\"https:\/\/www.afternerd.com\/blog\/python-lists-for-absolute-beginners\/\" target=\"_blank\" rel=\"noopener\">python list<\/a>.<\/p>\n<p>But first, let&#8217;s talk about what copying a list actually means.<\/p>\n<p>Let&#8217;s take a look at this example<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; a = [1, 2, 3, 4, 5]\r\n&gt;&gt;&gt; b = a\r\n&gt;&gt;&gt; a\r\n[1, 2, 3, 4, 5]\r\n&gt;&gt;&gt; b\r\n[1, 2, 3, 4, 5]<\/code><\/pre>\n<p>In the example above, is b a copy of a?<\/p>\n<p>The answer is actually <strong>No<\/strong><\/p>\n<p>b is in fact a, they both refer to the same python object.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-601\" src=\"http:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-not-copy-300x242.png\" alt=\"\" width=\"300\" height=\"242\" srcset=\"https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-not-copy-300x242.png 300w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-not-copy-768x619.png 768w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-not-copy-177x142.png 177w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-not-copy-200x161.png 200w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-not-copy-400x323.png 400w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-not-copy-600x484.png 600w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-not-copy-800x645.png 800w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-not-copy.png 1007w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>Let&#8217;s check what happens to b when we modify a.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; a[0] = 10\r\n&gt;&gt;&gt; b\r\n[10, 2, 3, 4, 5]<\/code><\/pre>\n<p>As you can see, changing the value of a[0] also changes the value of b[0] because they both refer to the same list object.<\/p>\n<p>So what does copying a list mean?<\/p>\n<p>Copying a python list means creating a <strong>new python object<\/strong>\u00a0whose contents are identical.<\/p>\n<p>The following figure shows what we want to achieve when we copy or clone a list.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-602 size-medium\" src=\"http:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-copy-300x263.png\" alt=\"\" width=\"300\" height=\"263\" srcset=\"https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-copy-300x263.png 300w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-copy-768x672.png 768w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-copy-1024x896.png 1024w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-copy-200x175.png 200w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-copy-400x350.png 400w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-copy-600x525.png 600w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-copy-800x700.png 800w, https:\/\/www.afternerd.com\/blog\/wp-content\/uploads\/2018\/04\/list-copy.png 1168w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/p>\n<p>In this article, we will discuss three different methods to copy a python list.<\/p>\n<p>The first two methods can be used in python2 and python3 whereas the third one works for python3 only.<\/p>\n<h2>First: Copying by Slicing<\/h2>\n<p>The most common way (especially in python2) to copy a python list is to use <a href=\"https:\/\/www.pythoncentral.io\/how-to-slice-listsarrays-and-tuples-in-python\/\" target=\"_blank\" rel=\"noopener\">slicing<\/a>.<\/p>\n<p>If you have been coding in python for a while, you probably came across some code that looks like this.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; b = a[:]\r\n<\/code><\/pre>\n<p>When you omit the start index and the end index from the slice, then your slice will start from the beginning of the list all the way to the end of the list.<\/p>\n<p>And because slicing creates a new object, then the above code effectively copies or clones the whole list into another list.<\/p>\n<p>Let&#8217;s go ahead and confirm this.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; a = [1, 2, 3, 7]\r\n&gt;&gt;&gt; b = a[:]\r\n&gt;&gt;&gt; b\r\n[1, 2, 3, 7]\r\n&gt;&gt;&gt; id(a)\r\n4440018888\r\n&gt;&gt;&gt; id(b)\r\n4440454712<\/code><\/pre>\n<p>This code confirms two things:<\/p>\n<p><em>1- the items of list <span class=\"symbol\">b<\/span> are the same as those of list <span class=\"symbol\">a<\/span><\/em><\/p>\n<p><em>2- <span class=\"symbol\">a<\/span> and <span class=\"symbol\">b<\/span> are different <strong>objects<\/strong><\/em><\/p>\n<p>But how do we know they are different?<\/p>\n<p>One way is by observing that <span class=\"symbol\">id(a)<\/span> is different from\u00a0<span class=\"symbol\">id(b)<\/span>.<\/p>\n<p>If you don&#8217;t know what the <span class=\"symbol\">id()<\/span> function does, it basically returns the address of a python object in the memory.<\/p>\n<p>Needless to say, two variables will refer to the same object only if the id of these two variable are exactly the same. Otherwise, they refer to different objects.<\/p>\n<p>You want to be more assured?<\/p>\n<p>Let&#8217;s try to modify <span class=\"symbol\">a<\/span> and see if <span class=\"symbol\">b<\/span>\u00a0remains unchanged.<\/p>\n<p>If <span class=\"symbol\">b<\/span> is modified, then <span class=\"symbol\">a<\/span> and <span class=\"symbol\">b<\/span> refer to the same object.<\/p>\n<p>If <span class=\"symbol\">b<\/span> remains unchanged, then <span class=\"symbol\">a<\/span> and <span class=\"symbol\">b<\/span> refer to two separate objects.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; a = [1, 2, 3, 7]\r\n&gt;&gt;&gt; b = a[:]\r\n&gt;&gt;&gt; b\r\n[1, 2, 3, 7]\r\n&gt;&gt;&gt; a[0] = -10\r\n&gt;&gt;&gt; a\r\n[-10, 2, 3, 7]\r\n&gt;&gt;&gt; b\r\n[1, 2, 3, 7]<\/code><\/pre>\n<p>As you can see, after <span class=\"symbol\">a<\/span> was modified, <span class=\"symbol\">b<\/span> remains unchanged.<\/p>\n<p>Awesome, we successfully copied a python list.<\/p>\n<h2>Second: Copying using list() function<\/h2>\n<p>Another way to create a copy of a list is to use the <span class=\"symbol\">list()<\/span> built-in function.<\/p>\n<p>The <span class=\"symbol\">list()<\/span> function is used to create a list object from any <strong>iterable<\/strong>.<\/p>\n<p>And most of the time in real code, this iterable is not a list.<\/p>\n<p>For example, the following code creates a <strong>new\u00a0<\/strong>list off of the items of a string.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; s = \"hello\"\r\n&gt;&gt;&gt; l = list(s)\r\n&gt;&gt;&gt; l\r\n['h', 'e', 'l', 'l', 'o']<\/code><\/pre>\n<p>But since a list is an iterable itself, there is nothing that prevents you from creating a list from another list.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; a = [1, 2, 3, 4]\r\n&gt;&gt;&gt; b = list(a)\r\n&gt;&gt;&gt; b\r\n[1, 2, 3, 4]\r\n&gt;&gt;&gt; id(a)\r\n4354322312\r\n&gt;&gt;&gt; id(b)\r\n4354377672<\/code><\/pre>\n<p>Even though this is not a common way to copy a list, it is still a valid one.<\/p>\n<h2>Third: Copying using the copy() Method<\/h2>\n<p>This is when Python3 comes to the rescue with a beautiful way to copy a list.<\/p>\n<p><strong>Python3<\/strong> introduced a new method to lists called <span class=\"symbol\">copy()<\/span> and it does exactly what you think it does.<\/p>\n<p>It copies a list into another list.<\/p>\n<pre class=\"prettyprint\"><code>&gt;&gt;&gt; a = [1, 2, 3, 4]\r\n&gt;&gt;&gt; b = a.copy()\r\n&gt;&gt;&gt; id(a)\r\n4354356936\r\n&gt;&gt;&gt; id(b)\r\n4354322312<\/code><\/pre>\n<p>The only downside is that it is not available in python2.<\/p>\n<p>But if you are using python3, there is no debate this is the best, and most readable way to copy a list.<\/p>\n<h2>Conclusion<\/h2>\n<p>If you are using <strong>python2<\/strong>, you can copy a list by slicing or by using the <span class=\"symbol\">list()<\/span> function.<\/p>\n<p>If you are using <strong>python3<\/strong>, use the list&#8217;s <span class=\"symbol\">copy()<\/span> 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":579,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[13],"yst_prominent_words":[319,39,224,244,246,245,251,252,322,160,321,229,158,240,253,249,235,318,234,324],"_links":{"self":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/570"}],"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=570"}],"version-history":[{"count":23,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/570\/revisions"}],"predecessor-version":[{"id":1267,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/posts\/570\/revisions\/1267"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media\/579"}],"wp:attachment":[{"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/media?parent=570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/categories?post=570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/tags?post=570"},{"taxonomy":"yst_prominent_words","embeddable":true,"href":"https:\/\/www.afternerd.com\/blog\/wp-json\/wp\/v2\/yst_prominent_words?post=570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}