{"id":4085,"date":"2020-03-20T13:59:26","date_gmt":"2020-03-20T13:59:26","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4085"},"modified":"2023-04-06T08:15:54","modified_gmt":"2023-04-06T08:15:54","slug":"copy-a-dictionary-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/dictionary\/copy-a-dictionary-in-python","title":{"rendered":"4 Easy Ways to Copy a Dictionary in Python"},"content":{"rendered":"\n<p><a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/dictionary\/python-dictionary-dict-tutorial\">A dictionary<\/a> in Python is an unordered collection of data that stores data in the form of <strong>key-value <\/strong>pair where each value can be accessed using its corresponding key.<\/p>\n\n\n\n<p>A dictionary can contain large sets of key-value pairs, manually copying this dictionary can be hard and time-consuming. In Python, we have various ways to copy a dictionary using a few lines of code.<\/p>\n\n\n\n<p>This tutorial will discuss the different methods or techniques to copy dictionaries in Python with examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Methods to Copy a Dictionary in Python<\/h2>\n\n\n\n<p>In this section, we are going to detail the 4 different methods by that one can copy a dictionary in Python. Let&#8217;s learn about them one by one.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. Copying Dictionary Element-by-element<\/h3>\n\n\n\n<p>In this technique, we loop through the entire dictionary and copy each element by its key into a new dictionary declared earlier.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#given dictionary\ndict1={0:&#039;1&#039;,1:&#039;2&#039;,3:&#x5B;1,2,3]}\nprint(&quot;Given Dictionary:&quot;,dict1)\n#new dictionary\ndict2={}\nfor i in dict1:\n    dict2&#x5B;i]=dict1&#x5B;i] #element by elemnet copying\n\nprint(&quot;New copy:&quot;,dict2)\n#Updating dict2 elements and checking the change in dict1\ndict2&#x5B;1]=33\ndict2&#x5B;3]&#x5B;1]=&#039;22&#039; #list item updated\nprint(&quot;Updated copy:&quot;,dict2)\nprint(&quot;Given Dictionary:&quot;,dict1)\n<\/pre><\/div>\n\n\n<p><strong>Output<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nGiven Dictionary: {0: &#039;1&#039;, 1: &#039;2&#039;, 3: &#x5B;1, 2, 3]}\nNew copy: {0: &#039;1&#039;, 1: &#039;2&#039;, 3: &#x5B;1, 2, 3]}\n\nUpdated copy: {0: &#039;1&#039;, 1: 33, 3: &#x5B;1, &#039;22&#039;, 3]}\nGiven Dictionary: {0: &#039;1&#039;, 1: &#039;2&#039;, 3: &#x5B;1, &#039;22&#039;, 3]}\n<\/pre><\/div>\n\n\n<p><strong>In the above code:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We initialized a dictionary dict1 and print it, <\/li>\n\n\n\n<li>Then we declare an empty dictionary dict2 where we are going to copy dict1,<\/li>\n\n\n\n<li>Next, we traverse through the dict1 using a <a class=\"rank-math-link\" href=\"https:\/\/www.askpython.com\/python\/python-for-loop\">for loop<\/a>. And using the operation <code>dict2[i]=dict1[i]<\/code>, we copy each and every element from dict1 to dict2.<\/li>\n<\/ul>\n\n\n\n<p><strong>Note: <\/strong>The = operator creates a reference to iterable objects in the dictionary. Therefore, if a non-iterable element in<strong> <\/strong>dict2 is updated, the corresponding element in dict1 is unchanged. Whereas, if an iterable element like a list item is changed, we see it modify the original dictionary <strong>dict1 <\/strong>list as well. The second part of the above code explains it. After updating try to compare both dict1 and dict2 results. We see that the above statement is true.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Using the = operator to Copy a Dictionary in Python<\/h3>\n\n\n\n<p>We have a quick way to copy a dictionary in Python using the &#8216;=&#8217; operator.<\/p>\n\n\n\n<p>Let us see with an example how we can directly copy a dictionary in Python using an &#8216;=&#8217; operator.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#given dictionary\ndict1={1:&#039;a&#039;,2:&#039;b&#039;,3:&#x5B;11,22,33]}\nprint(&quot;Given Dictionary:&quot;,dict1)\n#new dictionary\ndict2=dict1 #copying using = operator\nprint(&quot;New copy:&quot;,dict2)\n\n#Updating dict2 elements and checking the change in dict1\ndict2&#x5B;1]=33\ndict2&#x5B;3]&#x5B;2]=&#039;44&#039; #list item updated\n\nprint(&quot;Updated copy:&quot;,dict2)\nprint(&quot;Given Dictionary:&quot;,dict1)\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=\"\">\nGiven Dictionary: {1: &#039;a&#039;, 2: &#039;b&#039;, 3: &#x5B;11, 22, 33]}\n\nNew copy: {1: &#039;a&#039;, 2: &#039;b&#039;, 3: &#x5B;11, 22, 33]}\n\nUpdated copy: {1: 33, 2: &#039;b&#039;, 3: &#x5B;11, 22, &#039;44&#039;]}\nGiven Dictionary {1: 33, 2: &#039;b&#039;, 3: &#x5B;11, 22, &#039;44&#039;]}\n<\/pre><\/div>\n\n\n<p><strong>In the above code:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Firstly we initialize a dictionary dict1 and directly copy it to a new object dict2 by the code line <code>dict2=dict1<\/code>,<\/li>\n\n\n\n<li>This operation copies references of each object present in dict1 to the new dictionary dict2,<\/li>\n\n\n\n<li>Hence, updating any element of the dict2 will result in a change in dict1 and vice versa,<\/li>\n\n\n\n<li>This is clear from the above code that when we update any (iterable or non-iterable) object in dict2, we also see the same change in dict1.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. Using copy() Method<\/h3>\n\n\n\n<p><code>copy()<\/code> is a Python copy dictionary method returns a <strong>shallow copy <\/strong>of the given dictionary. It is similar to what we saw previously in the case of copying elements by traversing through a dictionary, that is, references of the dictionary elements are inserted into the new dictionary(Shallow copy). Look at the below example:<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#given dictionary\ndict1={ 10:&#039;a&#039;, 20:&#x5B;1,2,3], 30: &#039;c&#039;}\nprint(&quot;Given Dictionary:&quot;,dict1)\n#new dictionary\ndict2=dict1.copy() #copying using copy() method\nprint(&quot;New copy:&quot;,dict2)\n\n#Updating dict2 elements and checking the change in dict1\ndict2&#x5B;10]=10\ndict2&#x5B;20]&#x5B;2]=&#039;45&#039; #list item updated\n\nprint(&quot;Updated copy:&quot;,dict2)\nprint(&quot;Given Dictionary:&quot;,dict1)\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=\"\">\nGiven Dictionary: {10: &#039;a&#039;, 20: &#x5B;1, 2, 3], 30: &#039;c&#039;}\nNew copy: {10: &#039;a&#039;, 20: &#x5B;1, 2, 3], 30: &#039;c&#039;}\n\nUpdated copy: {10: 10, 20: &#x5B;1, 2, &#039;45&#039;], 30: &#039;c&#039;}\nGiven Dictionary: {10: &#039;a&#039;, 20: &#x5B;1, 2, &#039;45&#039;], 30: &#039;c&#039;}\n<\/pre><\/div>\n\n\n<p><strong>In the above code:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>We initialize a dictionary dict1 with some values. And use the <code>copy()<\/code> method on it to create a shallow copy,<\/li>\n\n\n\n<li>After the copy has been made, we update the new elements and see the corresponding change in the original dictionary,<\/li>\n\n\n\n<li>Similar to the case of the element-by-element copying technique, here too, change in non-iterable elements of dict2 does not have any effect on the original dictionary,<\/li>\n\n\n\n<li>Whereas for iterable ones like lists, the change is reflected in the given dictionary dict1 too<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. Using copy.deepcopy() Method to Copy a Dictionary in Python<\/h3>\n\n\n\n<p>We can use another Python dictionary copy method deepcopy() to copy a dictionary in Python. The <code>deepcopy()<\/code> method in Python is a member of the <strong>copy <\/strong>module. It returns a new dictionary with copied elements of the passed dictionary. Note, this method copies all the elements of the given dictionary in a <strong>recursive<\/strong> manner. Let us see how can use it,<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport copy\n\n#given dictionary\ndict1={ 10:&#039;a&#039;, 20:&#x5B;1,2,3], 30: &#039;c&#039;}\nprint(&quot;Given Dictionary:&quot;,dict1)\n#new dictionary\ndict2=copy.deepcopy(dict1) #copying using deepcopy() method\n\nprint(&quot;New copy:&quot;,dict2)\n#Updating dict2 elements and checking the change in dict1\ndict2&#x5B;10]=10\ndict2&#x5B;20]&#x5B;2]=&#039;45&#039; #list item updated\n\nprint(&quot;Updated copy:&quot;,dict2)\nprint(&quot;Given Dictionary:&quot;,dict1)\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=\"\">\nGiven Dictionary: {10: &#039;a&#039;, 20: &#x5B;1, 2, 3], 30: &#039;c&#039;}\nNew copy: {10: &#039;a&#039;, 20: &#x5B;1, 2, 3], 30: &#039;c&#039;}\n\nUpdated copy: {10: 10, 20: &#x5B;1, 2, &#039;45&#039;], 30: &#039;c&#039;}\nGiven Dictionary: {10: &#039;a&#039;, 20: &#x5B;1, 2, 3], 30: &#039;c&#039;}\n<\/pre><\/div>\n\n\n<p><strong>In the above code:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In the first line, we import the copy module,<\/li>\n\n\n\n<li>Then, we initialize the original dictionary dict1,<\/li>\n\n\n\n<li>We use the <code>copy.deepcopy()<\/code> method to copy dict1 elements in the new dictionary dict2,<\/li>\n\n\n\n<li>After successful copying, we update the new copy and see the changes in the original dictionary,<\/li>\n\n\n\n<li>As we can see from the output, any change in dict2 is not reflected in dict1. Hence, this method is useful when we need to change the new dictionary in our code while keeping the original one intact.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>In this tutorial, we have learned about the four different ways to copy a dictionary in Python: <strong>1. Copying Dictionary Element-by-element<\/strong>, <strong>2. Using = operator to Copy a Dictionary in Python<\/strong>, <strong>3. Using copy() Method<\/strong>, and<strong> 4. Using copy.deepcopy() Method to Copy a Dictionary in Python<\/strong>. The best way to choose any one of the above methods is to try it yourself and find out what works best for you. Hope you enjoyed reading this tutorial.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/docs.python.org\/3\/library\/copy.html\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.python.org\/3\/library\/copy.html<\/a><\/li>\n\n\n\n<li><a class=\"rank-math-link\" href=\"https:\/\/stackoverflow.com\/questions\/5861498\/fast-way-to-copy-dictionary-in-python\" target=\"_blank\" rel=\"noopener\"><\/a><a href=\"https:\/\/stackoverflow.com\/questions\/5861498\/fast-way-to-copy-dictionary-in-python\" target=\"_blank\" rel=\"noopener\">https:\/\/stackoverflow.com\/questions\/5861498\/fast-way-to-copy-dictionary-in-python<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>A dictionary in Python is an unordered collection of data that stores data in the form of key-value pair where each value can be accessed using its corresponding key. A dictionary can contain large sets of key-value pairs, manually copying this dictionary can be hard and time-consuming. In Python, we have various ways to copy [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":4184,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-4085","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dictionary"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4085","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\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4085"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4085\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4184"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}