How to merge dictionaries in Python?


The dictionary in Python is an unordered collection of key-value pairs with the key being unique, and it doesn't have append operation. If a key repeatedly appears in dictionary representation, only the last appearance is retained, as shown below.

Example: Dictionary with Unique Keys
>>> d1={"A1":20, "B1":30, "C1":40, "B1":25, "D1":50}
>>> d1
{'A1': 20, 'B1': 25, 'C1': 40, 'D1': 50}

This characteristic is used to merge key-value pairs of one dictionary in another. In the following example, while iterating over items() collection of one, if its key exists in other, its value will be updated, or else a new pair will be added.

Example: Merge Dictionaries using for Loop
d1={'A1': 20, 'B1': 25, 'C1': 40, 'D1': 50}
d2={"X1":100, "Y1":200, "b1":25, "A1":22,"D1":"Hello"}

for k,v in d2.items():
    d1[k]=v

print(d1)
Output
{'A1': 22, 'B1': 25, 'C1': 40, 'D1': 'Hello', 'X1': 100, 'Y1': 200, 'b1': 25}

Merge Dictionaries using dict.update()

This is an explicit technique of merging a dictionary with another. Same works under the hood with the update() method of the dict class.

Example: Merge Dictionaries using update()
>>> d1={'A1': 20, 'B1': 25, 'C1': 40, 'D1': 50}
>>> d2={"X1":100, "Y1":200, "b1":25, "A1":22,"D1":"Hello"}
>>> d1.update(d2)
>>> d1
{'A1': 22, 'B1': 25, 'C1': 40, 'D1': 'Hello', 'X1': 100, 'Y1': 200, 'b1': 25}

Merge Dictionaries using Unpacking Operator

Dictionaries can also be merged by using the unpacking operator (**). It is a unary operator with a dict object as an operand. It adds each k-v pair in an empty dictionary. Obviously, if the second dictionary is also unpacked, the value of the existing key will be updated.

Example: Merge Dictionaries using **
>>> d1={'A1': 20, 'B1': 25, 'C1': 40, 'D1': 50}
>>> d2={"X1":100, "Y1":200, "b1":25, "A1":22,"D1":"Hello"}
>>> d3={**d1, **d2}
>>> d3
{'A1': 22, 'B1': 25, 'C1': 40, 'D1': 'Hello', 'X1': 100, 'Y1': 200, 'b1': 25}

Merge Dictionaries using ChainMap()

Another approach for merging dictionaries is using the ChainMap() function in collections module. The ChainMap object is a dict-like object that creates a single view of multiple dictionaries.

Example: Merge Dictionaries using ChainMap()
>>> from collections import ChainMap
>>> d1={'A1': 20, 'B1': 25, 'C1': 40, 'D1': 50}
>>> d2={"X1":100, "Y1":200, "b1":25, "A1":22,"D1":"Hello"}
>>> map=ChainMap(d2,d1)
>>> map
ChainMap({'X1': 100, 'Y1': 200, 'b1': 25, 'A1': 22, 'D1': 'Hello'}, {'A1': 20, 'B1': 25, 'C1': 40, 'D1': 50})
>>> dict(map)
{'A1': 22, 'B1': 25, 'C1': 40, 'D1': 'Hello', 'X1': 100, 'Y1': 200, 'b1': 25}

Merge Dictionaries using Union Operator

Lastly, we shall have a look at the union operator ( | ) introduced in Python 3.9. This operator works on two dict object operands and returns the merged dictionary.

Example: Merge Dictionaries using Union Operator
>>> d1={'A1': 20, 'B1': 25, 'C1': 40, 'D1': 50}
>>> d2={"X1":100, "Y1":200, "b1":25, "A1":22,"D1":"Hello"}
>>> d1 | d2
{'A1': 22, 'B1': 25, 'C1': 40, 'D1': 'Hello', 'X1': 100, 'Y1': 200, 'b1': 25}

An in-place update operator ( |= ) has also been introduced in this version. Unlike the union operator, this doesn't create a new dictionary and updates the dictionary operand on the left.