Merging two Dictionaries | Python
The main operations on a Python dictionary are storing a value with some key and extracting the value given the key. In Python, there are many methods of merging two dictionaries . In this article you can learn different ways to merge two or more dictionaries. There is no built-in function to concatenate (merge) two dictionaries together in Python, but we can make some arrangements to do that. Some of the methods need a few lines of code to merge while one can combine the dictionaries in a single expression . So, let's now step up to see the different options to solve merge two dictionaries in a single expression in Python.
Using Double asterisks (**) Operator
PEP 448 also expanded the abilities of ** by allowing this operator to be used for dumping key/value pairs from one dictionary into a new dictionary . Leveraging dictionary comprehension and the unpacking operator, you can merge the two dictionaries in a single expression .- Python 3.5 or greater
>>>dict1 = {1:'one' , 2:'two'}
>>>dict2 = {3:'three', 4:'four'}
>>>fDict = {**dict1 , **dict2}
>>>print(fDict)
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
Using Python plus (+) Operator
If you are still working in Python 2.7 , then using the plus (+) sign to combine items from the two dictionaries produces a single expression solution.- Python 2.7

Using ChainMap
ChainMap is a data structure provided by the Python standard library that allows you to treat multiple dictionaries as one.- Python 3.0 and later
>>> from collections import ChainMap
>>> dict1 = {1:'one' , 2:'two'}
>>> dict2 = {3:'three', 4:'four'}
>>> fDict = dict(ChainMap({}, dict2, dict1))
>>> print(fDict)
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
Using Update() method
The update() method updates the dictionary with the elements from the another dictionary object or from an iterable of key/value pairs . The update() method adds element(s) to the dictionary if the key is not in the dictionary. If the key is in the dictionary, it updates the key with the new value .- Python 2, (or 3.4 or lower)

Using | operator
- Python 3.9 and later
>>> dict1 = {1:'one' , 2:'two'}
>>> dict2 = {3:'three', 4:'four'}
>>> fDict = dict1 dict2
>>> print(fDict)
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
Merge multiple dictionaries
If you want to merge more than one dictionary, the simple solution is :
>>> dict1 = {1:'one' , 2:'two'}
>>> dict2 = {3:'three', 4:'four'}
>>> dict3 = {5:'five', 6:'six'}
>>> fDict = {}
>>> fDict.update(dict1)
>>> fDict.update(dict2)
>>> fDict.update(dict3)
>>> print(fDict)
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'}
Using comprehension
Comprehensions in Python provide you with a short and concise way to construct new sequences using sequences which have been already defined. In pythonic way , you can use comprehension to merge two dictionaries .
>>> dict1 = {1:'one' , 2:'two'}
>>> dict2 = {3:'three', 4:'four'}
>>> fDict={i:items[i] for items in [dict1,dict2] for i in items}
>>> print(fDict)
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
Merge Dictionary using Lambda function
A lambda function can take any number of arguments, but can only have one expression. You can use lambda function to merge two dictionaries in Python.
>>> dict1 = {1:'one' , 2:'two'}
>>> dict2 = {3:'three', 4:'four'}
>>> fDict = (lambda f=dict1.copy(): (f.update(dict2), f)[1])()
>>> print(fDict)
{1: 'one', 2: 'two', 3: 'three', 4: 'four'}
Merge n number of Python Dictionaries
Python itertools module standardizes a core set of iterator building blocks. It has features like fast and memory efficient . The itertools.chain returns a chain object whose .next() method returns elements from the first iterable until it is exhausted, then the next iterable(s) , until all are exhausted. If you want to merge n number of Python Dictionaries the you can use the following:
import itertools
def union1(*dicts):
return dict(itertools.chain.from_iterable(dct.items() for dct in dicts))
dict1 = {1:'one' , 2:'two'}
dict2 = {3:'three', 4:'four'}
dict3 = {5:'five', 6:'six'}
dict4 = {7:'seven', 8:'eight'}
fDict = union1(dict1,dict2,dict3,dict4)
print(fDict)
output
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight'}
Related Topics