Merge Dictionaries in Python
In Python, you can merge two or more dictionaries in several ways, depending on the Python version you are using and how you want to handle duplicate keys.
Here are the most common methods:
Using The Union Operator (|)
This is the most modern and readable syntax for merging dictionaries, introduced in Python 3.9.
|creates a new dictionary containing the merged key-value pairs.|=updates the existing dictionary in place.
Example: Simple Merge
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
merged_dict = dict1 | dict2
print(merged_dict) # Output: {'a': 1, 'b': 3, 'c': 4}
If the key exists in both dictionaries, the value from the second dictionary overwrites the value from the first dictionary.
Example: In-Place Merge With |=
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
dict1 |= dict2
print(dict1) # Output: {'a': 1, 'b': 3, 'c': 4}
Using Dictionary Unpacking (**)
The unpacking operator (**) allows you to merge multiple dictionaries into a single one by expanding their key-value pairs. If the key exists in more than one dictionary, the value from the later dictionary overwrites the earlier one.
It was introduced in Python 3.5, and it creates a new dictionary.
For example:
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
merged_dict = {**dict1, **dict2}
print(merged_dict) # Output: {'a': 1, 'b': 3, 'c': 4}
Using The update() Method
The update() method modifies an existing dictionary by adding or updating key-value pairs from another dictionary. It doesn’t create a new dictionary.
For example:
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "c": 4}
dict1.update(dict2)
print(dict1) # Output: {'a': 1, 'b': 3, 'c': 4}
If you want to create a new dictionary using the update() method, first make a copy of the original dictionary with the copy() method, then use the update() method to add key-value pairs in the copy.
For example:
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "d": 4}
merged_dict = dict1.copy()
merged_dict.update(dict2)
print(merged_dict) # Output: {'a': 1, 'b': 3, 'd': 4}
Using Dictionary Comprehension
In Python, dictionary comprehension is a simple way to create a new dictionary by looping through existing data. You can also use it to merge two or more dictionaries by iterating through their key-value pairs and combining them into one.
For example:
dict1 = {"a": 1, "b": 2}
dict2 = {"b": 3, "d": 4}
merged_dict = {k: v for d in (dict1, dict2) for k, v in d.items()}
print(merged_dict) # Output: {'a': 1, 'b': 3, 'd': 4}
In this example, the outer loop (for d in (dict1, dict2)) goes through each dictionary in the tuple – first dict1, then dict2.
The inner loop (for k, v in d.items()) goes through all key-value pairs in the current dictionary.
For each pair, a new key and value are added to the new dictionary merged_dict. If the same key appears in both dictionaries, the value from the later dictionary (dict2) replaces the earlier one.
The result is a single merged dictionary merged_dict that combines all key-value pairs from both dictionaries dict1 and dict2.