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
| Parameter | Description |
|---|---|
other optional |
A mapping or an iterable of (key, value) pairs. |
**kwargs optional |
More keys as keyword arguments. |
Return value
None.
Examples
settings = {"theme": "light", "lang": "en"}
settings.update({"theme": "dark"}, size=14)
print(settings)
Output
{'theme': 'dark', 'lang': 'en', 'size': 14}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
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
dict.setdefault()· Return the value for a key; if the key is missing, insert it with a default value first and return that.dict.copy()· Return a shallow copy of the dictionary: a new dict with the same keys and values (nested values are shared).dict()· Create a dictionary: a mapping of keys to values. Build it from keyword arguments, pairs or another mapping.- Python Dictionaries · tutorial