While programming, there may be situations where we need to make an exact copy of a dictionary. In this article, we will look at different approaches to copy a dictionary in python and will implement those approaches.
Copy dictionary using for loop
We can create a copy of a python dictionary by iterating over the dictionary and adding the items of the current dictionary to a new dictionary. For this task, we can use the keys() method to first get a list of keys present in the dictionary and then we can insert the key and value pair into another dictionary. This can be done as follows.
myDict={1:1,2:4,3:9,4:16} print("Original Dictionary is:") print(myDict) newDict={} keyList=myDict.keys() for key in keyList: newDict[key]=myDict[key] print("Copied Dictionary is:") print(newDict)
Output:
Original Dictionary is: {1: 1, 2: 4, 3: 9, 4: 16} Copied Dictionary is: {1: 1, 2: 4, 3: 9, 4: 16}
While copying a dictionary using the above discussed method, references to the objects in the dictionary are copied. Hence, if there are mutable objects in the dictionary and we make any changes to the copied dictionary, the change will be reflected in the original dictionary. If we have a nested dictionary, when we make a change in any inner dictionary, the change will be reflected in both the copied and original dictionary. This can be seen in the following example.
myDict={1:{1:1},2:4,3:9,4:16} print("Original Dictionary is:") print(myDict) newDict={} keyList=myDict.keys() for key in keyList: newDict[key]=myDict[key] print("Copied Dictionary is:") print(newDict) newDict[1][1]=4 print("Copied Dictionary after change:") print(newDict) print("Original Dictionary after change:") print(myDict)
Output:
Original Dictionary is: {1: {1: 1}, 2: 4, 3: 9, 4: 16} Copied Dictionary is: {1: {1: 1}, 2: 4, 3: 9, 4: 16} Copied Dictionary after change: {1: {1: 4}, 2: 4, 3: 9, 4: 16} Original Dictionary after change: {1: {1: 4}, 2: 4, 3: 9, 4: 16}
- Python Programming – Creation, Initialization and Accessing The Elements In A Dictionary
- Python Programming – Dictionaries
- What is a Dictionary in Python?
Copy Dictionary using Dictionary comprehension
As we use list comprehension to copy lists, In a similar way we can use dictionary comprehension to copy dictionaries in python as follows.
myDict={1:1,2:4,3:9,4:16} print("Original Dictionary is:") print(myDict) newDict={key:value for (key,value) in myDict.items()} print("Copied Dictionary is:") print(newDict)
Output:
Original Dictionary is: {1: 1, 2: 4, 3: 9, 4: 16} Copied Dictionary is: {1: 1, 2: 4, 3: 9, 4: 16}
Again, the copied dictionary has references to objects in the original dictionary, when we have a nested dictionary and we make any changes to inner dictionary, the changes will be reflected in both the original and copied dictionary. This can be seen in the following example.
myDict={1:{1:1},2:4,3:9,4:16} print("Original Dictionary is:") print(myDict) newDict={key:value for (key,value) in myDict.items()} print("Copied Dictionary is:") print(newDict) newDict[1][1]=4 print("Copied Dictionary after change:") print(newDict) print("Original Dictionary after change:") print(myDict)
Output:
Original Dictionary is: {1: {1: 1}, 2: 4, 3: 9, 4: 16} Copied Dictionary is: {1: {1: 1}, 2: 4, 3: 9, 4: 16} Copied Dictionary after change: {1: {1: 4}, 2: 4, 3: 9, 4: 16} Original Dictionary after change: {1: {1: 4}, 2: 4, 3: 9, 4: 16}
Using dict.copy() method
We can use dict.copy() method to copy a dictionary. The method dict.copy() when invoked on a dictionary, returns a shallow copy of the original dictionary. We can copy the dictionary using dict.copy() as shown below.
myDict={1:1,2:4,3:9,4:16} print("Original Dictionary is:") print(myDict) newDict=myDict.copy() print("Copied Dictionary is:") print(newDict)
Output:
Original Dictionary is: {1: 1, 2: 4, 3: 9, 4: 16} Copied Dictionary is: {1: 1, 2: 4, 3: 9, 4: 16}
As the created copy of the dictionary is a shallow copy, the new dictionary has references to the objects of the original dictionary. When we make changes to any key value pair with primitive data types in any of the dictionaries, the change will not be shown in the other dictionary but when the dictionaries contain mutable objects in values, changes will be visible in both the original and copied dictionary. For example, if we have a nested dictionary copied using dict.copy() method and we make changes to the nested dictionary in the copied dictionary, the changes will be visible in the original dictionary. Similarly, when we make changes to the original dictionary, it will be reflected in the copied dictionary. This can be seen in the following example.
myDict={1:{1:1},2:4,3:9,4:16} print("Original Dictionary is:") print(myDict) newDict=myDict.copy() print("Copied Dictionary is:") print(newDict) newDict[1][1]=4 print("Copied Dictionary after change:") print(newDict) print("Original Dictionary after change:") print(myDict)
Output:
Original Dictionary is: {1: {1: 1}, 2: 4, 3: 9, 4: 16} Copied Dictionary is: {1: {1: 1}, 2: 4, 3: 9, 4: 16} Copied Dictionary after change: {1: {1: 4}, 2: 4, 3: 9, 4: 16} Original Dictionary after change: {1: {1: 4}, 2: 4, 3: 9, 4: 16}
Using copy.copy() method
Another way to create a shallow copy of a dictionary is by using the copy() method from the copy module. We pass the dictionary to be copied as an argument to the copy.copy() method and it creates a shallow copy of the dictionary passed as the argument and returns a reference to it. This can be done as follows.
import copy myDict={1:1,2:4,3:9,4:16} print("Original Dictionary is:") print(myDict) newDict=copy.copy(myDict) print("Copied Dictionary is:") print(newDict)
Output:
Original Dictionary is: {1: 1, 2: 4, 3: 9, 4: 16} Copied Dictionary is: {1: 1, 2: 4, 3: 9, 4: 16}
The dictionary created using copy.copy() method works in a similar way as the dictionary created using dict.copy() method. So, if there are nested dictionaries and we make a change to any inner dictionary, changes will be reflected in both the copied and original dictionary. This can be seen in the following example.
import copy myDict={1:{1:1},2:4,3:9,4:16} print("Original Dictionary is:") print(myDict) newDict=copy.copy(myDict) print("Copied Dictionary is:") print(newDict) newDict[1][1]=4 print("Copied Dictionary after change:") print(newDict) print("Original Dictionary after change:") print(myDict)
Output:
Original Dictionary is: {1: {1: 1}, 2: 4, 3: 9, 4: 16} Copied Dictionary is: {1: {1: 1}, 2: 4, 3: 9, 4: 16} Copied Dictionary after change: {1: {1: 4}, 2: 4, 3: 9, 4: 16} Original Dictionary after change: {1: {1: 4}, 2: 4, 3: 9, 4: 16}
Using copy.deepcopy() method
To create a copy of a dictionary which does not have references to the objects in the original dictionary we can use the deepcopy() method from the copy module as follows.
import copy myDict={1:1,2:4,3:9,4:16} print("Original Dictionary is:") print(myDict) newDict=copy.deepcopy(myDict) print("Copied Dictionary is:") print(newDict)
Output:
Original Dictionary is: {1: 1, 2: 4, 3: 9, 4: 16} Copied Dictionary is: {1: 1, 2: 4, 3: 9, 4: 16}
The deepcopy() method recursively visits all the objects in the dictionary and creates a new object for each element. No references are copied when we copy a dictionary using copy.deepcopy() method. When we make any changes to objects in the copied dictionary, even if it is a nested dictionary, no changes are made to the original dictionary. Similarly, when we make changes to the original dictionary, the changes are not reflected in the copied dictionary. This can be seen in the following example.
import copy myDict={1:{1:1},2:4,3:9,4:16} print("Original Dictionary is:") print(myDict) newDict=copy.deepcopy(myDict) print("Copied Dictionary is:") print(newDict) newDict[1][1]=4 print("Copied Dictionary after change:") print(newDict) print("Original Dictionary after change:") print(myDict)
Output:
Original Dictionary is: {1: {1: 1}, 2: 4, 3: 9, 4: 16} Copied Dictionary is: {1: {1: 1}, 2: 4, 3: 9, 4: 16} Copied Dictionary after change: {1: {1: 4}, 2: 4, 3: 9, 4: 16} Original Dictionary after change: {1: {1: 1}, 2: 4, 3: 9, 4: 16}
Conclusion
In this article, we have seen different ways to copy a dictionary in python. We have also seen that dict.copy() and copy.copy() methods create a copied dictionary which has references to the objects of the original dictionary while we can create a copied dictionary using copy.deepcopy() method which has no references to the elements in the original dictionary.Stay tuned for more informative articles.