How do I merge two dictionaries in a single expression in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Merging dictionaries in one expression is a common Python task, but the best syntax depends on your Python version and on whether you want a new dictionary or an in-place update. The core rule is simple: if the same key appears in both inputs, the right-hand dictionary wins unless you write custom conflict logic.
The Modern Answer in Python 3.9 and Later
Python 3.9 added the dictionary union operator |, which is now the clearest single-expression solution.
Output:
This creates a new dictionary and leaves the original dictionaries unchanged.
If you want an in-place update instead, use |=.
That mutates settings directly.
The Portable Expression for Python 3.5 and Later
Before Python 3.9, the most common one-expression answer was dictionary unpacking.
This also creates a new dictionary and keeps the same overwrite behavior: keys from right replace matching keys from left.
For many codebases that still support older Python versions, this remains the practical choice.
When a Single Expression Is Not Enough
Sometimes the question sounds simpler than the real requirement. If duplicate keys should be combined rather than overwritten, a plain merge operator is not enough.
Output:
This is still a single expression, but it is solving a different problem: conflict resolution rather than simple overwrite semantics.
update() Is Valid, but Not a Single Expression Result
You will often see update() in answers:
This is perfectly fine Python, but it is a two-step procedure. It is useful when you are okay with mutating an existing dictionary or when explicit steps are easier to read than a one-liner.
That is why update() is important to know, but it is not the cleanest answer if the question specifically asks for a single expression.
ChainMap Is Not a Real Merge
collections.ChainMap is sometimes mentioned in merge discussions, but it does not create a merged dictionary. It creates a layered mapping view.
This can be useful for configuration overlays, but it behaves differently from an actual merged dictionary. Use it when you want lookup precedence without copying everything immediately.
Common Pitfalls
One common mistake is forgetting overwrite order. In both left | right and {**left, **right}, the right-most value wins for duplicate keys. If you expect the left side to win, reverse the order.
Another mistake is using a one-expression merge when the real requirement is recursive merging of nested dictionaries. Normal dictionary merging is shallow. If both sides contain nested dictionaries under the same key, the entire nested value from the right side replaces the one on the left.
Developers also sometimes choose the most compact syntax without checking project compatibility. The | operator is excellent, but it requires Python 3.9 or later. In mixed-version environments, unpacking may be safer.
Finally, be careful with mutable values inside merged dictionaries. The merge creates a new outer dictionary, but inner objects such as lists and nested dicts are still shared references unless you copy them explicitly.
Summary
- In Python 3.9 and later,
left | rightis the clearest one-expression merge. - In Python 3.5 and later,
{**left, **right}is the common portable alternative. - Duplicate keys are resolved from right to left by default.
- Use custom logic if duplicate keys should be combined instead of overwritten.
- '
ChainMapis a layered view, not the same thing as a real merged dictionary.'

