merge

dict1 |= dict2

In-place merge operator (Python 3.9+) that updates d1 with entries from other, with other's values winning on shared keys. Unlike |, the right operand can be any iterable of pairs or a mapping — matching update()'s flexibility.

d1 |= other

Parameters

Parameter Purpose
left operand Dict mutated in place
right operand Mapping OR iterable of (key, value) pairs

Examples

>>> d = {'a': 1, 'b': 2}
>>> d |= {'b': 99, 'c': 3}
>>> d
{'a': 1, 'b': 99, 'c': 3}

Mutates d in place, right side wins

>>> d = {'a': 1}
>>> d |= [('b', 2), ('c', 3)]
>>> d
{'a': 1, 'b': 2, 'c': 3}

Accepts an iterable of pairs (unlike plain |)

>>> d = {'a': 1}
>>> ref = d
>>> d |= {'b': 2}
>>> ref
{'a': 1, 'b': 2}

In-place — aliases observe the change

>>> d = {'a': 1}
>>> d = d | {'b': 2}
>>> # equivalent RESULT to d |= ..., but rebinds instead of mutating

Contrast with |, which creates a new dict

Gotcha

Requires Python 3.9+. |= mutates in place like update() — a rebinding 'd = d | other' produces a new dict and does NOT affect other references.

Related methods

← All Python dict methods · String methods · Built-in functions