PyPython example

How to merge two dictionaries in Python

4 min read▶ Runs in an isolated hosted runtimeUpdated Jul 2026

Quick answer

In Python 3.9+, use the merge operator: a | b. It returns a new dict with both sets of keys, and when a key is in both, the value from the right-hand dict wins.

"Merging" means combining the keys of two dicts into one. The only real decisions are what happens on a key conflict (the second dict wins in every method here) and whether you want a new dict or to mutate an existing one in place. Each example runs on this page — hit Run, then edit the code and run it again.

1The | merge operator (3.9+)Recommended

Python 3.9 added | for dicts. a | b builds a brand-new dict from both, leaving the originals untouched — the cleanest, most readable option on any modern Python.

merge.py

Output

The right-hand side wins on conflicts, so b's port (9090) overrides a's. Both inputs are left unchanged.

2{**a, **b} unpacking

Dictionary unpacking spreads each dict's key/value pairs into a new literal. It also returns a fresh dict, and unlike | it works all the way back to Python 3.5 — reach for it when you can't rely on 3.9+.

merge.py

Output

Same result and same last-wins rule as |. It's handy for merging inline — {**defaults, **overrides, "extra": 1} — in a single expression.

3.update() (merge in place)

.update() is different: it mutates the left dict, copying the other dict's keys into it rather than returning a new one. Copy first if you need to keep the original.

merge.py

Output

We .copy() first so defaults stays intact. Drop the copy and .update() would overwrite defaults itself — occasionally what you want, often a bug.

4Which should you use?

MethodMutates?PythonBest for
a | bNo (new dict)3.9+Most cases
{**a, **b}No (new dict)3.5+Older Python / inline
.update()Yes (in place)AnyUpdating an existing dict

5Common variation: deep-merge nested dicts

All three methods are shallow: if a key holds a nested dict, the whole nested dict is replaced, not merged. To combine nested values, recurse — merge dicts key by key and only overwrite when the value isn't itself a dict:

deep_merge.py

Output

Note how db.host survives from base while db.port is overridden — a shallow base | override would have dropped host entirely.

Frequently asked questions

When two dictionaries share a key, which value wins?

The second (right-hand) dict wins for every method here. In a | b and {**a, **b} the later dict overrides the earlier; in a.update(b) the argument b overrides a. So merge order matters — put the dict whose values should take priority last.

Does merging modify the original dictionaries?

a | b and {**a, **b} build a new dict and leave both originals unchanged. .update() is different — it mutates the dict it is called on. Call .copy() first if you need to keep the original intact.

How do I merge more than two dictionaries?

Chain them: a | b | c (3.9+) or {**a, **b, **c}. Both apply left to right, so keys in later dicts override earlier ones.

How do I merge nested dictionaries?

The built-in methods are shallow, so a nested dict gets replaced wholesale. Write a small recursive deep_merge() that merges dicts key by key — see the variation above.