{"id":7239,"date":"2020-07-22T12:05:44","date_gmt":"2020-07-22T12:05:44","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7239"},"modified":"2022-08-06T13:15:00","modified_gmt":"2022-08-06T13:15:00","slug":"python-copy","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-copy","title":{"rendered":"Python Copy &#8211; Perform Deep and Shallow Copy"},"content":{"rendered":"\n<p>In this article, we&#8217;ll be looking at using the Python <em>Copy<\/em> module, to perform deep and shallow copy operations.<\/p>\n\n\n\n<p>Now, what do we mean by deep copy and shallow copy?<\/p>\n\n\n\n<p>Let&#8217;s take a look, using illustrative examples!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Why do we need the Python Copy module?<\/h2>\n\n\n\n<p>In Python, everything is represented using objects. Therefore, in a lot of cases, we may need to copy objects directly.<\/p>\n\n\n\n<p>In these cases, we cannot use the assignment operator directly.<\/p>\n\n\n\n<p>The point behind assignment is that multiple variables can point to the same object. This means that if the object changes using any of those variables, changes will be reflected everywhere!<\/p>\n\n\n\n<p>The following example illustrates this problem, using a shared <a href=\"https:\/\/www.askpython.com\/python\/list\/python-list\" class=\"rank-math-link\">list object<\/a>, which is mutable.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = &#x5B;1, 2, 3, 4]\n\nb = a\n\nprint(a)\nprint(b)\n\nb.append(5)\n\n# Changes will be reflected in a too!\nprint(a)\nprint(b)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;1, 2, 3, 4]\n&#x5B;1, 2, 3, 4]\n&#x5B;1, 2, 3, 4, 5]\n&#x5B;1, 2, 3, 4, 5]\n<\/pre><\/div>\n\n\n<p>As you can see, since both variables point to the same object, when <code>b<\/code> changes, so does <code>a<\/code>!<\/p>\n\n\n\n<p>To deal with this issue, Python gives us a way using the <em>Copy<\/em> module.<\/p>\n\n\n\n<p>The Python copy module is a part of the standard library, and can be imported using the below statement:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport copy\n<\/pre><\/div>\n\n\n<p>Now, in this module, we can perform two types of operations mainly:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Shallow Copy<\/li><li>Deep Copy<\/li><\/ul>\n\n\n\n<p>Let&#8217;s take a look at these methods now.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Shallow Copy<\/h2>\n\n\n\n<p>This method is used to perform a shallow copy operation.<\/p>\n\n\n\n<p>The syntax for calling this method is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport copy\n\nnew_obj = copy.copy(old_obj) # Perform a shallow copy\n<\/pre><\/div>\n\n\n<p>This will do two things &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Create a new object<\/li><li>Insert <em>all<\/em> references of the objects found in the original object<\/li><\/ul>\n\n\n\n<p>Now, since it creates a new object, we can be sure that our new object is different from the old object.<\/p>\n\n\n\n<p>However, this will still maintain references to nested objects. So if the object we need to copy has other mutable objects (list, set, etc), this will still maintain references to the same nested object!<\/p>\n\n\n\n<p>To understand this, let&#8217;s take an example.<\/p>\n\n\n\n<p>To illustrate the first point, we will try this with a simple list of integers (no nested objects!)<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport copy\n\nold_list = &#x5B;1, 2, 3]\n\nprint(old_list)\n\nnew_list = copy.copy(old_list)\n\n# Let&#039;s try changing new_list\nnew_list.append(4)\n\n# Changes will not be reflected in the original list, since the objects are different\nprint(old_list)\nprint(new_list)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;1, 2, 3]\n&#x5B;1, 2, 3, 4]\n&#x5B;1, 2, 3]\n<\/pre><\/div>\n\n\n<p>As you can see, in case our object is a simple list, there is no problem with shallow copy.<\/p>\n\n\n\n<p>Let&#8217;s take another case, where our object is a list of lists.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport copy\n\nold_list = &#x5B;&#x5B;1, 2], &#x5B;1, 2, 3]]\n\nprint(old_list)\n\nnew_list = copy.copy(old_list)\n\n# Let&#039;s try changing a nested object inside the list\nnew_list&#x5B;1].append(4)\n\n# Changes will be reflected in the original list, since the object contains a nested object\nprint(old_list)\nprint(new_list)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;1, 2], &#x5B;1, 2, 3]]\n&#x5B;&#x5B;1, 2], &#x5B;1, 2, 3, 4]]\n&#x5B;&#x5B;1, 2], &#x5B;1, 2, 3, 4]]\n<\/pre><\/div>\n\n\n<p>Here, notice that both <code>old_list<\/code> and <code>new_list<\/code> have been affected!<\/p>\n\n\n\n<p>If we must avoid this behavior, we must copy all objects recursively, along with nested objects.<\/p>\n\n\n\n<p>This is called a <em>Deep Copy<\/em> Operation using the Python copy module.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Deep Copy<\/h2>\n\n\n\n<p>This method is similar to the shallow copy method, but now copies everything from the original object (including nested objects) into a new object.<\/p>\n\n\n\n<p>To do a deep copy operation, we can use the below syntax:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport copy\n\nnew_object = copy.deepcopy(old_object)\n<\/pre><\/div>\n\n\n<p>Let&#8217;s take our old example, and try using deep copy to solve our issue.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport copy\n\nold_list = &#x5B;&#x5B;1, 2], &#x5B;1, 2, 3]]\n\nprint(old_list)\n\nnew_list = copy.deepcopy(old_list)\n\n# Let&#039;s try changing a nested object inside the list\nnew_list&#x5B;1].append(4)\n\n# Changes will be reflected in the original list, since the objects are different\nprint(old_list)\nprint(new_list)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;&#x5B;1, 2], &#x5B;1, 2, 3]]\n&#x5B;&#x5B;1, 2], &#x5B;1, 2, 3]]\n&#x5B;&#x5B;1, 2], &#x5B;1, 2, 3, 4]]\n<\/pre><\/div>\n\n\n<p>Notice that the old list is unchanged. Since all objects were copied recursively, there is no problem now!<\/p>\n\n\n\n<p>However, due to copying all objects, this deepcopy method is a bit more expensive, as compared to the shallow copy method.<\/p>\n\n\n\n<p>So use this wisely, only when you need it!<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned about using the Python Copy module, to perform shallow copy and deep copy operations.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Python Copy Module <a href=\"https:\/\/docs.python.org\/3.8\/library\/copy.html\" target=\"_blank\" rel=\"noopener\">Documentation<\/a><\/li><li>JournalDev article on Python Copy Module<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll be looking at using the Python Copy module, to perform deep and shallow copy operations. Now, what do we mean by deep copy and shallow copy? Let&#8217;s take a look, using illustrative examples! Why do we need the Python Copy module? In Python, everything is represented using objects. Therefore, in a [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":7241,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7239","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7239","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7239"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7239\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/7241"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}