Python Program to Concatenate Two Dictionaries Into One


When it is required to concatenate two dictionaries into a single entity, the ‘update’ method can be used.

A dictionary is a ‘key-value’ pair.

Below is a demonstration for the same −

Example

 Live Demo

my_dict_1 = {'J':12,'W':22}
my_dict_2 = {'M':67}
print("The first dictionary is :")
print(my_dict_1)
print("The second dictionary is :")
print(my_dict_2)
my_dict_1.update(my_dict_2)
print("The concatenated dictionary is :")
print(my_dict_1)

Output

The first dictionary is :
{'J': 12, 'W': 22}
The second dictionary is :
{'M': 67}
The concatenated dictionary is :
{'J': 12, 'W': 22, 'M': 67}

Explanation

Two dictionaries are defined, and are displayed on the console. 

  • The ‘update’ method is called on the first dictionary by passing second dictionary as parameter. 
  • This would help concatenate the dictionary. 
  • This is displayed on the console.

Updated on: 12-Mar-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements