7 ways to Merge multiple dictionaries in Python

Python Dictionary

In this post, we are going to learn 7 ways to Merge multiple dictionaries in Python. We will learn multiple techniques to do this operation. After reading this article, you will be able to use these techniques in your applications. So let us teach you these techniques one by one.

# Using update() method

The first technique is by making use of the update() method. The update() is an inbuilt function of the dictionary class, it takes an iterable of key-value pairs or dictionary as an argument and merges the passed dictionary contents in the current dictionary, and overwrite the existing key-value.

#Program to merge dictionaries in python
lang_Dict = {"C#":1, "C++":4, "Rat":8}
langkey = {'dog':67, 'cat':3, 'Rat':8}
Sub = {'Math':99, 'Eng':98}
lang_Dict.update(langkey)
lang_Dict.update(Sub)
print('merged python dictionaries =\n',lang_Dict)

Output

merged python dictionaries =
 {'C#': 1, 'C++': 4, 'Rat': 8, 'dog': 67, 'cat': 3, 'Math': 99, 'Eng': 98}

Using unpacking(**) operator

The next technique is by using the unpacking(**) operator. We can merge multiple dictionaries using the unpacking operator(**).We can do it in a single expression. It spread the contents of the dictionary as key-value pairs and in the end, you get the merged dictionary content.

#Program to merge dictionaries in python
lang_Dict = {"C#":1, "C++":4, "Rat":8}
lang_dict = {'dog':67, 'cat':3, 'Rat':8}
Sub_dict = {'Math':99, 'Eng':98}
merged_dict = {**lang_Dict , **lang_dict,**Sub_dict}
print('merged python dictionaries =\n',merged_dict)

Output

merged python dictionaries =
 {'C#': 1, 'C++': 4, 'Rat': 8, 'dog': 67, 'cat': 3, 'Math': 99, 'Eng': 98}

Using | operator merged dictionaries

The next technique is the | operator. The | merge operator was introduced in Python 3.9 in dict class. It is a simple and easy way to merged dictionaries. Let us understand it with a simple example.

#Program to merge dictionaries in python
lang_Dict = {"C#":1, "C++":4, "Rat":8}
animal_dict = {'dog':67, 'cat':3, 'Rat':8}
Sub_dict = {'Math':99, 'Eng':98}
merged_dict = lang_Dict | animal_dict
print('merged python dictionaries =\n',merged_dict)

Output

{'dog': 67, 'C++': 4, 'Rat': 8, 'C#': 1, 'cat': 3}

Using collections.ChainMap

The chainMap class in the collection module is used to merged two, or more dictionaries together. It creates a new dictionary without looping over dictionary items. We are using the chainMap class to merged dictionaries, later we are converting them to the dictionary,

#Program to merge dictionaries in python
from collections import ChainMap
lang_Dict = {"C#":1, "C++":4, "Rat":8}
animal_dict = {'dog':67, 'cat':3, 'Rat':8}
Sub_dict = {'Math':99, 'Eng':98}
merged_dict = dict(ChainMap(lang_Dict, animal_dict,Sub_dict))
print('merged python dictionaries =\n',merged_dict)

Output

merged python dictionaries =
 {'Math': 99, 'Eng': 98, 'dog': 67, 'cat': 3, 'Rat': 8, 'C#': 1, 'C++': 4}

Using itertools.chain()

“Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence” Python Docs

Code example

In this code example we are using the itertools.chain().

#Program to merge dictionaries in python
from itertools import chain
lang_Dict = {"C#":1, "C++":4, "Rat":8}
animal_dict = {'dog':67, 'cat':3, 'Rat':8}
Sub_dict = {'Math':99, 'Eng':98}
merged_dict = dict(chain(lang_Dict.items(), Sub_dict.items(),animal_dict.items()))
print('merged python dictionaries =\n',merged_dict)

Output

merged python dictionaries =
 {'C#': 1, 'C++': 4, 'Rat': 8, 'Math': 99, 'Eng': 98, 'dog': 67, 'cat': 3}

Using Concatenate list of dictionaries

In this code example, we are converting the dictionaries into lists and concatenating them by passing them to the dict() constructor of the dictionary class which creates a new dictionary from the merged dictionary.

#Program to merge dictionaries in python
lang_Dict = {"C#":1, "C++":4, "Rat":8}
animal_dict = {'dog':67, 'cat':3, 'Rat':8}
Sub_dict = {'Math':99, 'Eng':98}
merged_dict = dict(list(lang_Dict.items()) + list(animal_dict.items())+list(Sub_dict.items()))
print('merged python dictionaries =\n',merged_dict)

Output

merged python dictionaries =
 {'C#': 1, 'C++': 4, 'Rat': 8, 'dog': 67, 'cat': 3, 'Math': 99, 'Eng': 98}

Using dictionary Comprehension

In this code example, we are merging three dictionaries by using dictionary comprehension.

#Program to merge dictionaries in python
lang_Dict = {"C#":1, "C++":4, "Rat":8}
animal_dict = {'dog':67, 'cat':3, 'Rat':8}
Sub_dict = {'Math':99, 'Eng':98}
merged_dict = {key: value for dict in [lang_Dict, animal_dict,Sub_dict] for key, value in dict.items()}

print('merged python dictionaries =\n',merged_dict)

Output

merged python dictionaries =
 {'C#': 1, 'C++': 4, 'Rat': 8, 'dog': 67, 'cat': 3, 'Math': 99, 'Eng': 98}

Conclusion

In this post, we have understood, how to Merge multiple dictionaries in Python. We hope you enjoyed this post and find it useful for your applications.

Happy learning!!