Last modified: Jan 27, 2026 By Alexander Williams
Python Dict Merge: Combine Dictionaries Easily
Merging dictionaries is a common task. You often need to combine data from two sources. Python offers several clean ways to do this. This guide covers the main methods.
We will look at the update method, unpacking, and comprehensions. Each method has its use case. Understanding them makes your code better.
Using the Update Method
The update method is a built-in way to merge. It adds items from one dictionary into another. The original dictionary is changed. This is called an in-place operation.
Here is a basic example. We have two dictionaries. We want to merge dict2 into dict1.
# Define two dictionaries
dict1 = {"a": 1, "b": 2}
dict2 = {"c": 3, "d": 4}
# Use the update method
dict1.update(dict2)
print("Merged Dictionary:", dict1)
Merged Dictionary: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
The update method modifies dict1. Dict2 remains unchanged. If there are overlapping keys, the values from dict2 win. They overwrite the values in dict1.
For a deeper dive into this method, see our Python Dict Update Method Guide & Examples.
Using Dictionary Unpacking (Python 3.5+)
Dictionary unpacking uses the ** operator. It is a clean and modern syntax. It creates a new dictionary without changing the originals. This is often the preferred method.
dict1 = {"name": "Alice", "age": 30}
dict2 = {"city": "London", "job": "Engineer"}
# Merge using unpacking
merged_dict = {**dict1, **dict2}
print("New Merged Dictionary:", merged_dict)
print("Original dict1:", dict1) # Unchanged
print("Original dict2:", dict2) # Unchanged
New Merged Dictionary: {'name': 'Alice', 'age': 30, 'city': 'London', 'job': 'Engineer'}
Original dict1: {'name': 'Alice', 'age': 30}
Original dict2: {'city': 'London', 'job': 'Engineer'}
You can unpack more than two dictionaries. The last dictionary's value wins for duplicate keys. This method is very readable for simple merges.
Using a Dictionary Comprehension
Dictionary comprehensions offer control. You can filter or transform items during the merge. This is powerful for complex logic. It also creates a new dictionary.
First, let's see a simple merge comprehension.
dict1 = {"x": 10, "y": 20}
dict2 = {"y": 25, "z": 30} # Note duplicate key 'y'
# Merge with comprehension, dict2 values take priority
merged = {key: value for d in (dict1, dict2) for key, value in d.items()}
print("Merged with Comprehension:", merged)
Merged with Comprehension: {'x': 10, 'y': 25, 'z': 30}
The comprehension iterates through both dictionaries. The order matters. Since dict2 is last, its value for 'y' (25) is kept.
For more on this powerful feature, check our Python Dict Comprehension Guide & Examples.
Handling Key Conflicts
What if both dictionaries have the same key? You must decide which value to keep. The standard methods give priority to the second dictionary.
Sometimes you need a custom rule. For example, you might want to sum the values. A comprehension is perfect for this.
inventory1 = {"apples": 5, "oranges": 3}
inventory2 = {"apples": 2, "bananas": 7}
# Merge and sum values for common keys
merged_inventory = {}
# Add all items from first dict
for key, value in inventory1.items():
merged_inventory[key] = value
# Add items from second dict, summing if key exists
for key, value in inventory2.items():
if key in merged_inventory:
merged_inventory[key] += value
else:
merged_inventory[key] = value
print("Merged and Summed:", merged_inventory)
Merged and Summed: {'apples': 7, 'oranges': 3, 'bananas': 7}
This approach gives you full control. You can implement any logic for conflicts.
The Union Operator (Python 3.9+)
Python 3.9 introduced the union operator | for dictionaries. It provides a very intuitive syntax for merging. It creates a new dictionary.
# Requires Python 3.9 or later
config_defaults = {"theme": "light", "language": "en"}
user_config = {"language": "fr", "notifications": True}
merged_config = config_defaults | user_config
print("Merged Config:", merged_config)
Merged Config: {'theme': 'light', 'language': 'fr', 'notifications': True}
The | operator is clean and expressive. For in-place merging, you can use the |= operator. It works like update.
Remember, this only works in Python 3.9 and above. Check your Python version before using it.
Merging Multiple Dictionaries
You often need to merge more than two dictionaries. All the methods shown can be extended. Unpacking and the union operator handle this elegantly.
dict_a = {"a": 1}
dict_b = {"b": 2}
dict_c = {"c": 3}
# Method 1: Unpacking
merged_unpack = {**dict_a, **dict_b, **dict_c}
# Method 2: Union Operator (Python 3.9+)
# merged_union = dict_a | dict_b | dict_c
print("Merged Three Dicts:", merged_unpack)
Merged Three Dicts: {'a': 1, 'b': 2, 'c': 3}
You can also chain the update method. But it will modify the first dictionary. Be careful with that.
For more on working with dictionary keys during operations, our Python Dictionary Keys Guide & Examples is a great resource.
Conclusion
Merging dictionaries in Python is straightforward. You have several good options.
Use update for in-place modification. Use unpacking (**) for a clean, new dictionary. Use comprehensions for custom merge logic. Use the | operator if you use Python 3.9+.
The best method depends on your needs. Do you need a new dict? Do you have key conflicts? Choose the tool that fits.
Mastering these techniques makes your data handling code more efficient and readable. Practice with different examples to become comfortable.