How do I merge two dictionaries in a single expression in Python?
Categories:
Merging Dictionaries in Python: A Comprehensive Guide

Learn various Python techniques to merge two or more dictionaries into a single expression, covering different Python versions and use cases.
Merging dictionaries is a common operation in Python programming, allowing you to combine key-value pairs from multiple dictionaries into a single one. Python offers several elegant and concise ways to achieve this, especially with newer language versions. This article explores the most effective methods for merging dictionaries in a single expression, providing practical examples and considerations for each approach.
The Dictionary Unpacking Operator (Python 3.5+)
The dictionary unpacking operator (**
) is arguably the most Pythonic and readable way to merge dictionaries, especially when dealing with two or more. Introduced in Python 3.5 (PEP 448), it allows you to unpack dictionaries directly into a new dictionary literal. When duplicate keys are present, the value from the rightmost dictionary takes precedence.
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
merged_dict = {**dict1, **dict2}
print(merged_dict)
# Output: {'a': 1, 'b': 3, 'c': 4}
dict3 = {'d': 5}
merged_three = {**dict1, **dict2, **dict3}
print(merged_three)
# Output: {'a': 1, 'b': 3, 'c': 4, 'd': 5}
Merging dictionaries using the dictionary unpacking operator.
The Dictionary Union Operator (Python 3.9+)
Python 3.9 introduced the dictionary union operator (|
) and in-place update operator (|=
), providing an even more explicit and intuitive syntax for merging dictionaries. This operator behaves similarly to set union, combining key-value pairs. Like the unpacking operator, if keys overlap, the value from the right-hand operand's dictionary is used.
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
merged_dict = dict1 | dict2
print(merged_dict)
# Output: {'a': 1, 'b': 3, 'c': 4}
# In-place update
dict1 |= dict2
print(dict1)
# Output: {'a': 1, 'b': 3, 'c': 4}
Merging dictionaries using the dictionary union operator.
flowchart TD A[Start with dict1] --> B{dict1 | dict2} B --> C{Check for duplicate keys} C -->|No duplicates| D[Combine all key-value pairs] C -->|Duplicates exist| E[Use value from dict2 for duplicates] D --> F[Result: New merged dictionary] E --> F
Flowchart illustrating the dictionary union operator's logic.
Using dict.update()
(Modifies In-Place)
While not strictly a 'single expression' for creating a new dictionary without modification, dict.update()
is a fundamental method for merging dictionaries. It updates a dictionary with key-value pairs from another dictionary or from an iterable of key-value pairs. If a key already exists, its value is overwritten. This method modifies the dictionary on which it's called in-place and returns None
.
dict1 = {'a': 1, 'b': 2}
dict2 = {'b': 3, 'c': 4}
# To create a new dictionary without modifying dict1:
merged_dict = dict1.copy()
merged_dict.update(dict2)
print(merged_dict)
# Output: {'a': 1, 'b': 3, 'c': 4}
# Modifying dict1 in-place:
dict1.update(dict2)
print(dict1)
# Output: {'a': 1, 'b': 3, 'c': 4}
Merging dictionaries using dict.update()
.
dict.update()
modifies the dictionary it's called on. If you need to preserve the original dictionaries, make a copy first, as shown in the example.Choosing the Right Method
The best method depends on your Python version and whether you need to create a new dictionary or modify an existing one in-place. For creating a new merged dictionary in a single expression:
- Python 3.9+: Use the dictionary union operator (
|
). It's explicit and clear. - Python 3.5 - 3.8: Use the dictionary unpacking operator (
**
). It's concise and widely adopted. - Python 2.x / Older Python 3: You might need to fall back to
dict.copy()
followed byupdate()
, or usecollections.ChainMap
for a view of combined dictionaries without actual merging.

Comparison of dictionary merging methods across Python versions.