How to Merge Dictionaries in Python
Merge two or more dictionaries with the | operator, dictionary unpacking, or update() — understand how key conflicts resolve and which approach mutates.
Overview
To combine the entries of several dictionaries into one, Python gives you three tools: the | merge operator (Python 3.9+), dictionary unpacking {a, b}, and the update() method. The first two produce a brand-new dictionary and are the modern default; update() writes the second dictionary *into* the first, mutating it in place. All three resolve key conflicts the same way — when two dictionaries share a key, the value from the later one wins.
Steps
1. Merge with the | operator (Python 3.9+)
The | operator merges two dictionaries into a fresh one, copying every entry from the left operand and then the right. It never touches the sources, so you always get a new dictionary back. This is the cleanest, most readable option and the one to prefer on any modern Python.
a = {"a": 1}
b = {"b": 2}
merged = a | b
print(merged) # {'a': 1, 'b': 2}
2. Merge with dictionary unpacking
Spreading each dictionary into a new literal — {a, b} — copies all their entries into one fresh dict. It works on every Python 3 version, so reach for it when you must support an interpreter older than 3.9. The behavior is identical to |: a new dictionary, sources left untouched, later keys winning.
a = {"a": 1}
b = {"b": 2}
merged = {**a, **b}
print(merged) # {'a': 1, 'b': 2}
3. Merge in place with update()
a.update(b) copies every entry of b into a, mutating a and returning None. Use it when you deliberately want to grow an existing dictionary rather than build a new one — accumulating results in a loop, say. If a is shared or you need the original preserved, copy it first (a.copy().update(...) in two steps) or use |.
a = {"a": 1}
b = {"b": 2}
a.update(b)
print(a) # {'a': 1, 'b': 2} - 'a' was mutated in place
4. Know which key wins on a conflict
When both dictionaries hold the same key, the later dictionary overwrites the earlier one — the same rule for all three approaches. That is what makes merging perfect for applying overrides on top of defaults: list the defaults first and the overrides last, so the overrides win.
defaults = {"theme": "light", "size": "md"}
prefs = {"theme": "dark"}
print(defaults | prefs) # {'theme': 'dark', 'size': 'md'}
5. Merge a variable number of dictionaries
When the dictionaries arrive in a list, fold them left to right with reduce so the rightmost value still wins on any shared key.
from functools import reduce
dicts = [{"a": 1}, {"b": 2}, {"a": 9, "c": 3}]
merged = reduce(lambda acc, d: acc | d, dicts, {})
print(merged) # {'a': 9, 'b': 2, 'c': 3}
A few equivalent one-liners for cases you will hit often — chaining more than two, the keyword-unpacking form, and the shallow-merge trap with nested values:
a = {"a": 1}
b = {"b": 2}
c = {"c": 3}
combined = a | b | c # chains left to right; the rightmost dict wins on conflicts
triple = {**a, **b, **c} # same result on any Python 3
kw = dict(**a, **b, **c) # dict(**...) also works, but every key must be a string
user = {"u": {"name": "Ada"}}
extra = {"u": {"age": 36}}
deep = user | extra # {'u': {'age': 36}} -- the whole 'u' is replaced, NOT deep-merged
# to actually combine the inner dicts you must merge them yourself:
fixed = {"u": user["u"] | extra["u"]} # {'u': {'name': 'Ada', 'age': 36}}
base = {"x": 1}
snapshot = base.copy() # copy first when you must keep the original intact
snapshot.update({"y": 2}) # mutate the copy, not base
# base is still {'x': 1}; snapshot is {'x': 1, 'y': 2}
Runnable example
The complete, runnable version — merge {'a': 1} and {'b': 2} and confirm the sources survive:
a = {"a": 1}
b = {"b": 2}
merged = a | b
print(merged) # {'a': 1, 'b': 2}
print(a) # {'a': 1} - source untouched
print(b) # {'b': 2} - source untouched
Common pitfalls
The | operator needs Python 3.9 or newer; on anything older it raises TypeError: unsupported operand type(s). When your target runtime might be older, fall back to {a, b}, which behaves identically and runs everywhere on Python 3.
Frequently asked questions
What is the easiest way to merge two dictionaries in Python?
merged = a | b. It returns a new dictionary and, on key conflicts, keeps the value from the rightmost dictionary.Which value wins when merged dictionaries share a key?
a | b, {a, b}, and a.update(b), b's value overwrites a's for any shared key.Does merging dictionaries modify the originals?
| and {a, b} never mutate the sources — they return a new dict. a.update(b) mutates a in place, so copy first if you need the original preserved.