How To Merge Two Dictonaries In Python?

Python dictionary is a popular type used to store key and value pairs. Multiple dictionaries can be merged in different ways in Python. As dictionary items consist of key and value pairs merge operation is not straightforward as a list. In this tutorial, we examine different ways to merge two or more dictionaries in Python.

Merge Two Dictionaries with update() Method

The official and most reliable way to merge two dictionaries is using the update() method provided by the dictionary. The first dictionary can call the update() method by providing the second dictionary as a parameter and the first dictionary is updated with provided second dictionary items. The syntax of the update() method is like below.

DICTIONARY1.update(DICTIONARY2)

In the following example, we merge two dictionaries.

d1= {'a':'1','b':'2'}

d2= {'c':'3','d':'4'}

d1.update(d2)

print(d1)
{'a': '1', 'b': '2', 'c': '3', 'd': '4'}

Merge Two Dictionaries with ** (kwargs) Operator

The ** or kwargs operator is used to extracting items from provided list or dictionary. This can be used to merge two dictionaries by extracting two dictionary items and putting them in another dictionary that contains all items. The syntax of the ** kwargs operator to merge two dictionaries are like below.

MERGE_DICTIONARY = {**DICTIONARY1,**DICTIONARY2}

In the following example, we merge two dictionaries named d1 and d2 into dictionary named merge_dictionary .

d1= {'a':'1','b':'2'}

d2= {'c':'3','d':'4'}

merged_dictionary = {**d1,**d2}

Merge Two Dictionaries with | (Pipe) Operator

Python is a progressive language that adds new features in every new version. With Python 3.9 the | pipe operator is provided for the dictionaries to merge them easily like a mathematical operation. The syntax of the | pipe operator to merge dictionaries is like below.

d1= {'a':'1','b':'2'}

d2= {'c':'3','d':'4'}

merged_dictionary = d1 | d2

Merge Two Dictionaries with List Conversion

Another way to merge two dictionaries is using the list conversion. Dictionaries can be converted into lists and then put together and convert into dictionaries again. In the following example, we merge two dictionaries named d1 and d2 by converting them to a list and putting them into a single dictionary named merged_dictionary .

d1= {'a':'1','b':'2'}

d2= {'c':'3','d':'4'}

merged_dictionary = dict(list(d1.items())+list(d2.items()))

print(merged_dictionary)

Merge More Than Two Dictionaries

If we need to merge more than two dictionaries with a single step previously used methods can be used. ** kwargs operator, | pipe operator, and list conversion can be used to merge more than two dictionaries.

d1= {'a':'1','b':'2'}

d2= {'c':'3','d':'4'}

d3= {'e':'5','f':'6'}

#Using kwargs ** Operator
merged_dictionary = dict(**d1,**d2,**d3)
print(merged_dictionary)


#Using Pipe | Operator
merged_dictionary = d1 | d2 | d3
print(merged_dictionary)


#Using List Conversion
merged_dictionary = dict(list(d1.items())+list(d2.items())+list(d3.items()))
print(merged_dictionary)

Leave a Comment