Python developers commonly use dictionary to store large amounts of data as key-value pairs. They can be easily converted into JSON data and vice versa, making it easy to send data between browsers & servers. Sometimes, you may need to copy data from one dictionary to another in Python. There are several ways to do this. In this article, we will learn how to copy data from one dictionary to another in Python.
Shallow Copy vs Deep Copy
Before we proceed, we need to understand the difference between shallow copy and deep copy. A shallow copy creates a new object but maintains the object references within the original. In this case, if you update object references in original, it will be reflected in the shallow copy too. A deep copy is completely independent of the original. Even the object references point to new copies of objects. In this case, if you make any changes to the original object, it will not affect the copy. To create a deep copy, Python first creates a new object and then recursively insert copies of objects present in original object.
How to Copy Data from One Dictionary to Another in Python
Here are the different ways to copy data from one dictionary to another in Python.
1. Using copy() method
Every dictionary supports copy() method that creates & returns a shallow copy of the dictionary. Changes made to the original will not affect the copy, unless you modify the object references. Here is an example to demonstrate its use.
d1 = {"a": 1, "b": 2, "c": 3}
d2 = d1.copy()
print(d2) # Output: {'a': 1, 'b': 2, 'c': 3}
2. Using dict() constructor
The dict constructor is used to create a new dictionary. It can also be used to create a shallow copy of the existing dictionary.
d1 = {"a": 1, "b": 2, "c": 3}
d2 = dict(d1)
print(d2) # Output: {'a': 1, 'b': 2, 'c': 3}
3. Using update() method
In both the above solutions, the entire dictionary is cloned completely. If you copy data from one dictionary to another using these methods, then the target dictionary will be completely overwritten.
If you want to add content of one dictionary to another, without erasing its existing content, then use update() function.
d1 = {"a": 1, "b": 2}
d2 = {"c": 3, "d": 4}
d1.update(d2)
print(d1) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Every dictionary supports update() function. You need to pass an argument with the dictionary whose key-value pairs you need to add to the target dictionary.
If there are any common keys between two dictionaries then the value of the source dict provided as argument in update() function, will overwrite the value in target dict.
d1 = {"a": 1, "b": 2, "c": 4}
d2= {"c": 3, "d": 4}
d1.update(d2)
print(d1) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
In the above example, the dicts d1 and d2 have a common key ‘c’. After you copy data, its value in d1 will be that of its value in dict d2.
4. Using unpacking operator
Since Python 3.5, you can also use unpacking operator(**) to copy values from one dictionary to another. It is a very concise way to copy data across dictionaries.
d1 = {"a": 1, "b": 2}
d2 = {"c": 3, "d": 4}
d3 = {**d1, **d2}
print(d3) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
5. Using deepcopy method
In most cases, you can simply create a shallow copy to get your work done. But sometimes you may need to create a deep copy instead. Python provides a copy library that offers deepcopy() to create a deep copy of an object. This copy is completely independent of the source.
import copy
d1 = {"a": 1, "b": 2}
d2 = copy.deepcopy(d1)
print(d2) # Output: {'a': 1, 'b': 2}
Conclusion
In this article, we have learnt several ways to copy data from one dictionary to another. If you want to copy the entire dictionary, you can use copy() method or dict() constructor. If you want to add items of one dict to another without completely wiping out the target dict’s items, then you can use update() method. All these solutions give you a shallow copy of dict. If you want to perform a deep copy of a dict, then you can use deepcopy() function. It will result in a dict that is completely independent of original dict.
Also read:
How to Convert String to List in Python
How to Check if Variable is Defined in Python
How to Reverse String in Python

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.