How to Merge Two Dictionaries in Python
In this Python example we will discuss some of the ways to merge two dictionaries as this is one of the most common operations in Python.
Some of the topics which will be helpful for understanding the program implementation better are:
1. Using the update Method
One of the easiest way to merge the dictionaries is to use update method. This method updates the first dictionary with the key-value pairs from the second dictionary.
Now let’s implement a program to merge dictionaries using update method. After emerging the result dictionary will have all the key values from both the initial dictionaries.
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
dict1.update(dict2)
print(dict1)Output
{'a': 1, 'b': 2, 'c': 3, 'd': 4}2. Using Dictionary Unpacking (**) Operator
Another simple way is to use dictionary unpacking operator. This operator unpacks the key-value pairs from one dictionary and adds them to another dictionary.
Now let’s implement a program to merge dictionaries using dictionary unpacking operator operator.
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged_dict = {**dict1, **dict2}
print(merged_dict)Output
{'a': 1, 'b': 2, 'c': 3, 'd': 4}3. Using the dict Constructor
The dict constructor can also be used to merge two dictionaries in Python. This method creates a new dictionary by combining the key-value pairs from two dictionaries.
Now let’s implement a program to merge dictionaries using dict constructor.
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged_dict = dict(dict1, **dict2)
print(merged_dict)Output
{'a': 1, 'b': 2, 'c': 3, 'd': 4}4. Using the ChainMap Function
The ChainMap function from the collections module can also be used to merge two dictionaries in Python. This function creates a view of all the dictionaries in a list and allows you to access their key-value pairs as if they were a single dictionary.
Now let’s implement a program to merge dictionaries using ChainMap function.
from collections import ChainMap
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged_dict = ChainMap(dict1, dict2)
print(merged_dict)Output
ChainMap({'a': 1, 'b': 2}, {'c': 3, 'd': 4})5. Using the merge Method
The merge method from the dict class creates a new dictionary by combining the key-value pairs from two dictionaries.
Now let’s implement a program to merge dictionaries using merge method.
dict1 = {'a': 1, 'b': 2}
dict2 = {'c': 3, 'd': 4}
merged_dict = dict1.copy()
merged_dict.update(dict2)
print(merged_dict)Output
{'a': 1, 'b': 2, 'c': 3, 'd': 4}4. Conclusion
In this Python example, we discussed multiple ways to merge two dictionaries. We explored five different methods for merging dictionaries, including using the update method, the ** operator, the dict constructor, the ChainMap function, and the merge method.
Helpful Links
Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.
Also for examples in Python and practice please refer to Python Examples.
Complete code samples are present on Github project.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?