{"id":367,"date":"2024-03-18T14:20:18","date_gmt":"2024-03-18T14:20:18","guid":{"rendered":"https:\/\/offensivepython.com\/?p=367"},"modified":"2024-03-18T15:06:19","modified_gmt":"2024-03-18T15:06:19","slug":"copying-lists","status":"publish","type":"post","link":"https:\/\/offensivepython.com\/index.php\/2024\/03\/18\/copying-lists\/","title":{"rendered":"Copying Lists"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"367\" class=\"elementor elementor-367\">\n\t\t\t\t<div class=\"elementor-element elementor-element-4a39925 e-flex e-con-boxed e-con e-parent\" data-id=\"4a39925\" data-element_type=\"container\" data-e-type=\"container\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-a37ec52 elementor-widget elementor-widget-text-editor\" data-id=\"a37ec52\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>\u00a0 \u00a0 \u00a0Ocasionally, it is useful to copy a list that we already have created in order to initialize a new list that is going to be used for another purpose.\u00a0 We need the original list to remain intact with modifications taking place in the new list.\u00a0 How to copy a list and preserve the original?<\/p><p>\u00a0 \u00a0 \u00a0It may seem easy, but if we use this first method shown below, we are going to get ourselves in trouble fast, because it doesn&#8217;t copy the list.\u00a0 It only creates a new list variable pointing to the same place in memory.\u00a0\u00a0<\/p><p># Method 1 is incorrect for copying lists!<br \/><strong>list1 = [&#8220;one&#8221;, &#8220;two&#8221;, &#8220;three&#8221;]<\/strong><br \/><strong>list2 = list1<\/strong><br \/><strong>list2.append(&#8220;four&#8221;)<\/strong><br \/><strong>print(&#8220;list1 has:&#8221;, list1)<\/strong><br \/><strong>print(&#8220;list2 has:&#8221;, list2)<\/strong><\/p><p>Output from Method 1:<\/p><p>list1 has: [&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;, &#8216;four&#8217;]<br \/>list2 has: [&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;, &#8216;four&#8217;]<\/p><p>As you can see, four was added to both lists.\u00a0 This is not what we want!\u00a0\u00a0<\/p><p>\u00a0 \u00a0 \u00a0We can actually use slicing to create a copy of a list.\u00a0 Simply slice from the beginning of a list to the end and save that to a new variable.\u00a0 Now you have a copy of a list.<\/p><p># Method 2 uses slicing and works great for copying a list.<br \/><strong>list1 = [&#8220;one&#8221;, &#8220;two&#8221;, &#8220;three&#8221;]<\/strong><br \/><strong>list2 = list1[:]<\/strong><br \/><strong>list2.append(&#8220;four&#8221;)<\/strong><br \/><strong>print(&#8220;list1 has:&#8221;, list1)<\/strong><br \/><strong>print(&#8220;list2 has:&#8221;, list2)<\/strong><\/p><p>Output from Method 2:<\/p><p>list1 has: [&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;]<br \/>list2 has: [&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;, &#8216;four&#8217;]<\/p><p>This time, four was only added to the new list.\u00a0 The old list is preserved, exactly what we want.\u00a0<\/p><p>\u00a0 \u00a0 \u00a0There is another method to use, which accomplishes the same thing, and that is to use the list function.\u00a0 Below is how it works.<\/p><p># Method 3 uses the list function to help accomplish our goal\u00a0<br \/><strong>list1 = [&#8220;one&#8221;, &#8220;two&#8221;, &#8220;three&#8221;]<\/strong><br \/><strong>list2 = list(list1)<\/strong><br \/><strong>list2.append(&#8220;four&#8221;)<\/strong><br \/><strong>print(&#8220;list1 has:&#8221;, list1)<\/strong><br \/><strong>print(&#8220;list2 has:&#8221;, list2)<\/strong><\/p><p>Output from Method 3:<\/p><p>list1 has: [&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;]<br \/>list2 has: [&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;, &#8216;four&#8217;]<\/p><p>As you can see, this worked fine as well.<\/p><p>\u00a0 \u00a0 \u00a0However, what happens if we have a nested list.\u00a0 How do we copy that?<\/p><p># Previous working methods do NOT work for copying a nested list!<br \/><strong>list1 = [[&#8220;one&#8221;, &#8220;two&#8221;, &#8220;three&#8221;], [&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;]]<\/strong><br \/><strong>list2 = list1[:]<\/strong><br \/><strong>list2.append([&#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;])<\/strong><br \/><strong>print(&#8220;list1 has:&#8221;, list1)<\/strong><br \/><strong>print(&#8220;list2 has:&#8221;, list2)<\/strong><\/p><p>Output from the first part of the code:<\/p><p>list1 has: [[&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;], [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;]]<br \/>list2 has: [[&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;], [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;], [&#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;]]<\/p><p>\u00a0 \u00a0 \u00a0This appears to have worked, but it only works if modifying the outer list.\u00a0 Continue reading to see what happens when we try to modify an inner list.<\/p><p># Continuation of code above<br \/><strong>list2[0].append(&#8220;four&#8221;)<\/strong><br \/><strong>print(&#8220;list1 has:&#8221;, list1)<\/strong><br \/><strong>print(&#8220;list2 has:&#8221;, list2)<\/strong><\/p><p>Output from modifying an inner list:<\/p><p>list1 has: [[&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;, &#8216;four&#8217;], [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;]]<br \/>list2 has: [[&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;, &#8216;four&#8217;], [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;], [&#8216;A&#8217;, &#8216;B&#8217;, &#8216;C&#8217;]]<\/p><p>Notice that four was added to the first inner list of list1 and list2.\u00a0 This is not what we want.<\/p><p>\u00a0 \u00a0 \u00a0In order to copy a nested list and preserve the original, we must import the <strong>deepcopy<\/strong> function from Python&#8217;s <strong>copy<\/strong> module and use it.<\/p><p># Using the deepcopy function from the copy module allows us to safely copy a nested list<br \/><strong>from copy import deepcopy<\/strong><br \/><strong>list1 = [[&#8220;one&#8221;, &#8220;two&#8221;, &#8220;three&#8221;], [&#8220;a&#8221;, &#8220;b&#8221;, &#8220;c&#8221;]]<\/strong><br \/><strong>list2 = deepcopy(list1)<\/strong><br \/><strong>list2[0].append(&#8220;four&#8221;)<\/strong><br \/><strong>print(&#8220;list1 has:&#8221;, list1)<\/strong><br \/><strong>print(&#8220;list2 has:&#8221;, list2)<\/strong><\/p><p>Output from the deepcopy code:<\/p><p>list1 has: [[&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;], [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;]]<br \/>list2 has: [[&#8216;one&#8217;, &#8216;two&#8217;, &#8216;three&#8217;, &#8216;four&#8217;], [&#8216;a&#8217;, &#8216;b&#8217;, &#8216;c&#8217;]]<\/p><p>As you can see, this time, four was only added to our copy.\u00a0 The original list is preserved.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 \u00a0Ocasionally, it is useful to copy a list that we already have created in order to initialize a new list that is going to be used for another purpose.\u00a0 We need the original list to remain intact with modifications taking place in the new list.\u00a0 How to copy a list and preserve the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-container-style":"default","site-container-layout":"default","site-sidebar-layout":"default","site-transparent-header":"default","disable-article-header":"default","disable-site-header":"default","disable-site-footer":"default","disable-content-area-spacing":"default","footnotes":""},"categories":[27],"tags":[30,29,28],"class_list":["post-367","post","type-post","status-publish","format-standard","hentry","category-compound-data-types","tag-copy","tag-deepcopy","tag-list"],"_links":{"self":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/comments?post=367"}],"version-history":[{"count":20,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":387,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/posts\/367\/revisions\/387"}],"wp:attachment":[{"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/offensivepython.com\/index.php\/wp-json\/wp\/v2\/tags?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}