Signup/Sign In
LAST UPDATED: AUGUST 9, 2021

Python program to Merging two Dictionaries

In this tutorial, we will learn to write a program to merge two dictionaries in Python. For two given dictionaries, we have to merge them and return a single dictionary that will have the values of both the dictionaries. Merging simply means that we are joining the two ditionaries together.

Let us look at the sample input and output to understand better.

Input:

d1= { 'A': 1, 'B': 2 }

d2= { 'x': 3, 'y': 4 }

Output: { 'A': 1, 'B': 2, 'x': 3, 'y': 4 }

To execute this task in Python, we can follow these approaches:

  1. using ** (double star) operator
  2. using update() method

Approach 1: using **(double star) operator

The ** operator is used to pass multiple parameters to a function with the help of a dictionary. This operator will be used to pass the key-value pairs of the first dictionary in a new dictionary and then we will pass the key-value pairs of the second dictionary in the new dictionary. Since a key is unique, all the duplicate keys of the first dictionary will be removed in the merged dictionary.

Algorithm

Follow the algorithm to understand the approach better.

Step 1- Initialise two dictionaries with values

Step 2- Declare a new dictionary that will store the merged values

Step 3- Use ** to pass values of first dictionary and second dictionary

Step 4- Print the new dictionary as the merged dictionary

Python Program 1

In this program, we have used the ** operator to pass all the key-value pairs of both the dictionaries one by one in the new dictionary. This will merge the dictionary, and we can print this result.

dic1= {'x': 3, 'y' : 8, 'z': 5 }
dic2= {1: 8, 'x': 4, 2: 6}

merge_dic= {**dic1,**dic2}

print("MERGED: ")
print(merge_dic)


MERGED:
{'x': 4, 'y': 8, 'z': 5, 1: 8, 2: 6}

Approach 2: using update() method

The update() method of the dictionary class inserts the values of a dictionary to another dictionary along with the respective keys. This method does not create a new dictionary but only adds elements to the existing dictionary.

Algorithm

Let us look at the algorithm to understand the working of the program.

Step 1- Initialise two dictionaries with values

Step 2- Add the values of the second dictionary in the first dictionary using the update() method

Step 3- Print the first dictionary which will have values of both dictionary

Python Program 2

Understand the implementation of the above-mentioned approach with the help of this code. We have defined two dictionaries with keys and values and then added the values of the second dictionary to the first dictionary to merge them.

dic1= {'x': 2, 'y' : 8, 'C': 5 }
dic2= {'B': 8, 'y': 4, 2: 11}

dic1.update(dic2)

print("MERGED: ")
print(dic1)


MERGED:
{'x': 2, 'y': 4, 'C': 5, 'B': 8, 2: 11}

Conclusion

In this tutorial, we have learned how to merge two dictionaries in Python using the update() method and the ** (double star) operator. We have discussed both approaches in detail.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.