Python 3 reference · Dictionary Methods

Python Dictionary update() Method

Add or overwrite keys from another mapping, an iterable of pairs, or keyword arguments, in place.

Python 3 Since 3.0

Syntax

dict.update(other=(), /, **kwargs)

Parameters

ParameterDescription
other optional A mapping or an iterable of (key, value) pairs.
**kwargs optional More keys as keyword arguments.

Return value

None.

Examples

Merge in placeTry it yourself
settings = {"theme": "light", "lang": "en"}
settings.update({"theme": "dark"}, size=14)
print(settings)

Output

{'theme': 'dark', 'lang': 'en', 'size': 14}
The | and |= operatorsTry it yourself
base = {"a": 1}
print(base | {"b": 2}, base)   # new dict (3.9+)
base |= {"c": 3}             # in place, like update()
print(base)

Output

{'a': 1, 'b': 2} {'a': 1}
{'a': 1, 'c': 3}

Version history

VersionChange
Python 3.0Added