Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

How to merge dictionaries in Python?

Merge Two Dict

This article covers all the methods to merge dictionaries in Python. Dictionaries are a convenient way to store data in Python. They store data in the form of key-value pairs.

While working with dictionaries you might want to merge two dictionaries.

Different Methods to Merge Dictionaries in Python

When it comes to merging two dictionaries there is more than one method of doing so.

In this tutorial, we will go over the different methods you can use to merge two dictionaries.

1. Using for loop

You can use a for loop to copy the entries of one dictionary to another. This is the naive way of merging two dictionaries.

You have to iterate over one dictionary using a for loop and add the entries to the other dictionary simultaneously.

The python code to do so is as follows :

dict1 = {  'Rahul': 4, 'Ram': 9, 'Jayant' : 10 }
dict2 = {  'Jonas': 4, 'Niel': 9, 'Patel' : 10 }

print("Before merging")
print("dictionary 1:", dict1)
print("dictionary 2:", dict2)

dict3 = dict1.copy()

for key, value in dict2.items():
    dict3[key] = value

print("after updating :")
print(dict3)
Merge Dictionaries

2. Using .update()

Dictionaries in Python have an in-built method for merging two dictionaries. You can call this method on one of the dictionaries and pass the other dictionary as an argument.

This can be done using the following lines of code:

dict1.update(dict2)

The complete code would be as follows :

dict1 = {  'Rahul': 4, 'Ram': 9, 'Jayant' : 10 }
dict2 = {  'Jonas': 4, 'Niel': 9, 'Patel' : 10 }

print("Before merging")
print("dictionary 1:", dict1)
print("dictionary 2:", dict2)

dict1.update(dict2)
print("after updating :")
print(dict1)
Merge Dictionaries 1

One disadvantage of using the update method is that you can only pass one dictionary as an argument. This limits you to merging only two dictionaries at once.

You can merge more than one dictionaries together using the ** (kwargs) operator. We will see this next.

3. Using **kwargs

Kwargs aka unpacking operator (**) also allows you to merge two or more dictionaries.

Kwargs is short for Keyword Arguments. It lets you send variable length key-value pairs.

To merge dictionaries using ** use the following line of code:

dict3 = {**dict1, **dict2}

The complete code is as follows :

Merge Dictionaries 2

You can use the same method to merge more than two dictionaries as well.

dict3 = {**dict1, **dict2, **dict3}

The complete code is as follows :

dict1 = {  'Rahul': 4, 'Ram': 9, 'Jayant' : 10 }
dict2 = {  'Jonas': 4, 'Niel': 9, 'Patel' : 10 }
dict3 = {  'John': 8, 'Naveen': 11, 'Ravi' : 15 }

print("Before merging")
print("dictionary 1:", dict1)
print("dictionary 2:", dict2)
print("dictionary 3:", dict3)

dict3 = {**dict1, **dict2, **dict3}
print("after updating :")
print(dict3)

Merge 3 Dictionaroes
Merge 3 Dictionaries

4. Using merge operator

The best way to merge dictionaries is by using the merge operator. It makes it very simple to perform a merge operation.

You can merge two dictionaries by using the following line of code.

dict1 |= dict2

The complete code is as follows:

dict1 = {  'Rahul': 4, 'Ram': 9, 'Jayant' : 10 }
dict2 = {  'Jonas': 4, 'Niel': 9, 'Patel' : 10 }

print("Before merging")
print("dictionary 1:", dict1)
print("dictionary 2:", dict2)

dict1 |= dict2
print("after updating :")
print(dict1)
Merge Dictionaries 3

Conclusion

This tutorial covered four different methods that you can use to merge dictionaries in python. Hope you had fun learning with us!