4 Easy Ways to Copy a Dictionary in Python

Copy a Dictionary In Python

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 a dictionary using a few lines of code.

This tutorial will discuss the different methods or techniques to copy dictionaries in Python with examples.

Methods to Copy a Dictionary in Python

In this section, we are going to detail the 4 different methods by that one can copy a dictionary in Python. Let’s learn about them one by one.

1. Copying Dictionary Element-by-element

In this technique, we loop through the entire dictionary and copy each element by its key into a new dictionary declared earlier.

Example:

#given dictionary
dict1={0:'1',1:'2',3:[1,2,3]}
print("Given Dictionary:",dict1)
#new dictionary
dict2={}
for i in dict1:
    dict2[i]=dict1[i] #element by elemnet copying

print("New copy:",dict2)
#Updating dict2 elements and checking the change in dict1
dict2[1]=33
dict2[3][1]='22' #list item updated
print("Updated copy:",dict2)
print("Given Dictionary:",dict1)

Output:

Given Dictionary: {0: '1', 1: '2', 3: [1, 2, 3]}
New copy: {0: '1', 1: '2', 3: [1, 2, 3]}

Updated copy: {0: '1', 1: 33, 3: [1, '22', 3]}
Given Dictionary: {0: '1', 1: '2', 3: [1, '22', 3]}

In the above code:

  • We initialized a dictionary dict1 and print it,
  • Then we declare an empty dictionary dict2 where we are going to copy dict1,
  • Next, we traverse through the dict1 using a for loop. And using the operation dict2[i]=dict1[i], we copy each and every element from dict1 to dict2.

Note: The = operator creates a reference to iterable objects in the dictionary. Therefore, if a non-iterable element in 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 dict1 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.

2. Using the = operator to Copy a Dictionary in Python

We have a quick way to copy a dictionary in Python using the ‘=’ operator.

Let us see with an example how we can directly copy a dictionary in Python using an ‘=’ operator.

Example:

#given dictionary
dict1={1:'a',2:'b',3:[11,22,33]}
print("Given Dictionary:",dict1)
#new dictionary
dict2=dict1 #copying using = operator
print("New copy:",dict2)

#Updating dict2 elements and checking the change in dict1
dict2[1]=33
dict2[3][2]='44' #list item updated

print("Updated copy:",dict2)
print("Given Dictionary:",dict1)

Output:

Given Dictionary: {1: 'a', 2: 'b', 3: [11, 22, 33]}

New copy: {1: 'a', 2: 'b', 3: [11, 22, 33]}

Updated copy: {1: 33, 2: 'b', 3: [11, 22, '44']}
Given Dictionary {1: 33, 2: 'b', 3: [11, 22, '44']}

In the above code:

  • Firstly we initialize a dictionary dict1 and directly copy it to a new object dict2 by the code line dict2=dict1,
  • This operation copies references of each object present in dict1 to the new dictionary dict2,
  • Hence, updating any element of the dict2 will result in a change in dict1 and vice versa,
  • 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.

3. Using copy() Method

copy() is a Python copy dictionary method returns a shallow copy 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:

Example:

#given dictionary
dict1={ 10:'a', 20:[1,2,3], 30: 'c'}
print("Given Dictionary:",dict1)
#new dictionary
dict2=dict1.copy() #copying using copy() method
print("New copy:",dict2)

#Updating dict2 elements and checking the change in dict1
dict2[10]=10
dict2[20][2]='45' #list item updated

print("Updated copy:",dict2)
print("Given Dictionary:",dict1)

Output:

Given Dictionary: {10: 'a', 20: [1, 2, 3], 30: 'c'}
New copy: {10: 'a', 20: [1, 2, 3], 30: 'c'}

Updated copy: {10: 10, 20: [1, 2, '45'], 30: 'c'}
Given Dictionary: {10: 'a', 20: [1, 2, '45'], 30: 'c'}

In the above code:

  • We initialize a dictionary dict1 with some values. And use the copy() method on it to create a shallow copy,
  • After the copy has been made, we update the new elements and see the corresponding change in the original dictionary,
  • 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,
  • Whereas for iterable ones like lists, the change is reflected in the given dictionary dict1 too

4. Using copy.deepcopy() Method to Copy a Dictionary in Python

We can use another Python dictionary copy method deepcopy() to copy a dictionary in Python. The deepcopy() method in Python is a member of the copy 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 recursive manner. Let us see how can use it,

Example:

import copy

#given dictionary
dict1={ 10:'a', 20:[1,2,3], 30: 'c'}
print("Given Dictionary:",dict1)
#new dictionary
dict2=copy.deepcopy(dict1) #copying using deepcopy() method

print("New copy:",dict2)
#Updating dict2 elements and checking the change in dict1
dict2[10]=10
dict2[20][2]='45' #list item updated

print("Updated copy:",dict2)
print("Given Dictionary:",dict1)

Output:

Given Dictionary: {10: 'a', 20: [1, 2, 3], 30: 'c'}
New copy: {10: 'a', 20: [1, 2, 3], 30: 'c'}

Updated copy: {10: 10, 20: [1, 2, '45'], 30: 'c'}
Given Dictionary: {10: 'a', 20: [1, 2, 3], 30: 'c'}

In the above code:

  • In the first line, we import the copy module,
  • Then, we initialize the original dictionary dict1,
  • We use the copy.deepcopy() method to copy dict1 elements in the new dictionary dict2,
  • After successful copying, we update the new copy and see the changes in the original dictionary,
  • 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.

Summary

In this tutorial, we have learned about the four different ways to copy a dictionary in Python: 1. Copying Dictionary Element-by-element, 2. Using = operator to Copy a Dictionary in Python, 3. Using copy() Method, and 4. Using copy.deepcopy() Method to Copy a Dictionary in Python. 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.

References