Instagram
youtube
Facebook
Twitter

Python Program to Merge Two Dictionaries into One

A Python Program to Merge Two Dictionaries into One?

Code Explanation:

Dictionary Declaration:
dict1 and dict2 are two separate dictionaries created with different key-value pairs.

Merging Logic:
merged_dict = {**dict1, **dict2} uses unpacking to merge the two dictionaries.
The ** operator extracts the contents of each dictionary.

Merge Result:
If both dictionaries have unique keys, all key-value pairs will be included in the result.

Output Display:
print() is used to display the final merged dictionary on the screen.

 

Program:

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

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

merged_dict = {**dict1, **dict2}

print("Merged dictionary:", merged_dict)