Merge two Python dictionaries in one expression

Use dictionary union or unpacking for a new mapping, understand right-hand precedence, and choose explicitly when values should be combined instead of replaced.

Best practices
Illustration of software components, data, and application workflows

For ordinary dictionaries on Python 3.9 or later, use merged = left | right. On Python 3.5 or later, {**left, **right} provides dictionary unpacking. Both create a new dictionary, with the right-hand value winning when a key appears in both inputs.

Create a new dictionary

left = {"firstname": "John"}
right = {"lastname": "Doe"}

merged = {**left, **right}
# {'firstname': 'John', 'lastname': 'Doe'}

merged = left | right  # Python 3.9+

The outer dictionaries are unchanged, but the merge is shallow: mutable values remain shared references unless you copy them separately.

Update an existing dictionary

left.update(right)
# left now contains both fields; update() returns None.

left |= right is the in-place union form. Although update() is a method-call expression, its return value is None; assigning merged = left.update(right) will not give you a merged dictionary. For standard dicts, | expects dictionaries, while update() and |= also accept supported mappings or iterables of key/value pairs.

Decide which values should win

left = {"a": 1, "b": 2}
right = {"b": 99, "c": 3}

print(left | right)
# {'a': 1, 'b': 99, 'c': 3}

print({**right, **left})
# {'b': 2, 'c': 3, 'a': 1}

Replacing an existing value does not move that key to the end. New keys follow their insertion order.

For several dictionaries, later operands keep the same precedence:

a = {"a": 1}
b = {"b": 2}
c = {"a": 3}
merged = {**a, **b, **c}
merged = a | b | c

FAQ

Can values be added instead of overwritten?

Yes. For numeric values and a missing-key default of zero, a comprehension can express that policy:

left = {"a": 2, "b": -1}
right = {"a": 3, "c": 4}
summed = {key: left.get(key, 0) + right.get(key, 0)
          for key in left.keys() | right.keys()}

The set of keys does not preserve the inputs' order. Counter(left) + Counter(right) is another option for counts, but it discards zero and negative results; it is not equivalent to arbitrary numeric addition.

Does union recursively merge nested dictionaries?

No. A conflicting nested value is replaced as a whole. A recursive merge needs an explicit policy for dictionaries, lists, conflicting types, and shared references; implement that policy or choose a library whose behavior matches it.

Why does dict union fail on Python 3.8?

The operator was introduced in 3.9. Use unpacking on 3.5–3.8. Keep version requirements visible rather than assuming every Python 3 installation supports the same syntax and operations.

References: Python dictionary operations, Python collections.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting