Python Program to Merge Two Dictionaries

Write a Python Program to Merge Two Dictionaries with a practical example. There are two options to achieve the same. The first option uses the built-in update() function, and the other option uses **.

Python Program to Merge Two Dictionaries Example

In this program, we use the dictionary.update() function to update first_Dict with second_Dict values.

first_Dict = {1: 'apple', 2: 'Banana' , 3: 'Orange'}
second_Dict = { 4: 'Kiwi', 5: 'Mango'}
print("First : ", first_Dict)
print("Second : ", second_Dict)

first_Dict.update(second_Dict)
    
print("\nAfter Concatenating : ")
print(first_Dict)
using update() function

Merge two dictionaries using the | operator

In the latest version (3.9), we can use the | operator to merge two dictionaries. It is the fastest way to create a new dictionary by merging two existing ones.

d1 = {1: 'apple', 2: 'banana'}
d2 = {3: 'mango', 4: 'pineapple'}

result = d1 | d2
print(result)
{1: 'apple', 2: 'banana', 3: 'mango', 4: 'pineapple'}

If there are duplicate keys, the | operator picks the second dictionary value as the final result for that particular key. For example, the following program sets the 2 dictionary key-value to mango.

d1 = {1: 'apple', 2: 'banana'}
d2 = {2: 'mango', 4: 'pineapple'}

result = d1 | d2
print(result)
{1: 'apple', 2: 'mango', 4: 'pineapple'}

Merge two Dictionaries using **

It is another way to merge in Python. In this program, we are using the dict keyword to create a new Python Dictionary using first_Dict and ** second_Dict. Here, ** allows you to pass multiple arguments.

first_Dict = {'a': 'apple', 'b': 'Banana' , 'o': 'Orange'}
second_Dict = { 'k': 'Kiwi', 'm': 'Mango'}
print("First : ", first_Dict)
print("Second : ", second_Dict)

print("\nAfter Concatenating : ")
print(dict(first_Dict, **second_Dict) )
using ** operator

Concatenate Two Dictionaries using functions

This code is the same as above. However, using a function, we separated the two dictionaries’ concatenation logic in this program. Refer to the Python programs.

def Merge_Dictionaries(first, second):
    result = {**first_Dict, **second_Dict}
    return result

first_Dict = {'a': 'apple', 'b': 'Banana' , 'o': 'Orange'}
second_Dict = { 'k': 'Kiwi', 'm': 'Mango'}
print("First Dictionary: ", first_Dict)
print("Second Dictionary: ", second_Dict)

# Concatenate Two Dictionaries 
third_Dict = Merge_Dictionaries(first_Dict, second_Dict)

print("\nAfter Concatenating two Dictionaries : ")
print(third_Dict)
Program to Merge Two Dictionaries 3

Program to merge two dictionaries using a for loop

First, we shallow copy the first dictionary to the result dict. Next, the program uses a for loop to iterate over the second dictionary’s items and add them to the final dictionary.

d1 = {1: 'apple', 2: 'banana'}
d2 = {3: 'mango', 4: 'pineapple'}

result = d1.copy()
print(result)

for key, value in d2.items():
result[key] = value

print(result)
{1: 'apple', 2: 'banana'}
{1: 'apple', 2: 'banana', 3: 'mango', 4: 'pineapple'}

If you don’t want to use a for loop, we can utilize the update() method after copy() to merge two dictionaries. Refer to the Python dict.update and Python dict.copy.

d1 = {1: 'apple', 2: 'banana'}
d2 = {3: 'mango', 4: 'pineapple'}

result = d1.copy()
result.update(d2)
print(result)
{1: 'apple', 2: 'banana', 3: 'mango', 4: 'pineapple'}

Also Read

  1. Python Program to Multiply All Items in a Dictionary
  2. Python Program to Remove Given Key from a Dictionary