Python Program to Merge Two Dictionaries


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co

In Python, a dictionary is a built-in data type that allows you to store a collection of key-value pairs. Each key in the dictionary maps to a corresponding value, which can be any Python object, including other dictionaries.


Python Code :

The below Python program that merges two dictionaries:


# Define two dictionaries
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'d': 4, 'e': 5, 'f': 6}

# Merge the dictionaries using the update() method
dict1.update(dict2)

# Print the merged dictionary
print(dict1)

Output:


{'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5, 'f': 6}

In this program, we first define two dictionaries dict1 and dict2. We then use the update() method of dict1 to merge dict2 into dict1. Finally, we print the merged dictionary dict1.

Note that if there are keys that are present in both dict1 and dict2, the values from dict2 will overwrite the values from dict1.


10xer.co Ad Sponsored

ⓘ Sponsored by 10xer.co