Python 3.9+Dictionaries (dict)term
d1 |= d2
operators
The in-place dictionary update operator (Python 3.9+). It adds or overwrites every pair of d2 in d1 without creating a new object.
Syntax
d1 |= d2
The update happens in place, so every name referring to that dict sees the change — including the caller, if the dict was passed into a function. Compared with |, the |= form is more permissive: the right operand may be any iterable of key-value pairs, not just a dict, exactly like update(). Before Python 3.9 the operator does not exist; use update() there.
Examples
config = {'debug': False, 'host': 'localhost'}
overrides = {'debug': True, 'port': 8080}
config |= overrides
print(config)
# → {'debug': True, 'host': 'localhost', 'port': 8080}
See also
Open in the full glossarydocs.python.orgReport an inaccuracy