The merge operator (Python 3.9+)

a = {"x": 1, "y": 2}
b = {"y": 9, "z": 3}
merged = a | b
print(merged)   # {"x": 1, "y": 9, "z": 3}

This is the cleanest modern approach and leaves both originals untouched.

The double-star way (works on older Python)

merged = {**a, **b}
print(merged)   # {"x": 1, "y": 9, "z": 3}

Unpacking both dictionaries into a new one gives the same result and works on Python 3.5 and up.

Merge in place with update()

a.update(b)
print(a)   # {"x": 1, "y": 9, "z": 3}

update() changes a directly rather than creating a new dictionary, so use it when you want to modify the original.

Which method should you use?

  • a | b — the clearest choice on Python 3.9 or newer.
  • {**a, **b} — when you need to support older versions.
  • a.update(b) — when you want to mutate a in place.

Frequently asked questions

Which value wins when a key is in both?

The right-hand dictionary wins. In a | b and {**a, **b}, any duplicate key takes its value from b.

Does merging change the original dictionaries?

The | operator and {**a, **b} create a new dictionary and leave the originals alone. update() is the exception — it modifies the first dictionary.

Dictionaries are everywhere in Python — get fluent with them in our free Python course.