4 Easy Ways to Copy a Dictionary in Python

Copy a Dictionary In Python

Introduction

In this tutorial, we are going to discuss the various methods or techniques to copy a dictionary in Python.

Theoretically, Dictionary in Python is an unordered collection of data values which stores corresponding elements as key-item pair. Further, each item can be accessed by the user using its corresponding key.

So, let us get right into the copying procedures.

Methods to Copy a Dictionary in Python

In this section, we are going to elaborate on the 4 different ways with which one can copy a dictionary in Python. Let’s go over them one by one.

1. Copying Dictionary Element-by-element

In this technique, we traverse throughout the whole dictionary and copy each and every element pointed by the key to a new dictionary which was previously declared. Look at the code below:

#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]}

Here,

  • We initialized a dictionary, dict1
  • After we print it, 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.

Now that our copying is done, remember that the = operator creates references for iterable objects in a dictionary. So, if a non-iterable element in dict2 is updated, the corresponding element in dict1 is kept intact.

Whereas, if an iterable object like list item is changed, we see a change in dict1 list too. The second part of the code above explains this. Try comparing both dict1 and dict2 results after updating. We see the above statement is true.

2. Using = operator to Copy a Dictionary in Python

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

#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 code above,

  • 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

The dictionary’s copy() method in Python 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 code below:

#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 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

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,

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'}

Now,

  • In the first line, 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.

Conclusion

So, in this tutorial, we learned about the 4 different ways to copy a dictionary in Python. For any questions regarding the topic feel free to use the comments below.

References