mutation
dict.update
Updates the dictionary in place with key/value pairs from other, overwriting existing keys. Accepts another mapping, an iterable of key/value pairs, and/or keyword arguments; returns None.
d.update([other]) Parameters
| Parameter | Purpose |
|---|---|
| other (mapping) | Another dict whose entries are merged in |
| other (iterable) | Iterable of (key, value) pairs, e.g. list of 2-tuples |
| **kwargs | Keyword arguments become key=value entries |
Examples
>>> d = {'a': 1}
>>> d.update({'b': 2, 'a': 99})
>>> d
{'a': 99, 'b': 2} Existing keys are overwritten
>>> d = {}
>>> d.update([('x', 1), ('y', 2)])
>>> d
{'x': 1, 'y': 2} Accepts an iterable of pairs
>>> d = {'a': 1}
>>> d.update(b=2, c=3)
>>> d
{'a': 1, 'b': 2, 'c': 3} Keyword arguments work too (keys must be valid identifiers)
>>> d = {'a': 1}
>>> print(d.update({'b': 2}))
None update returns None — it mutates in place
Gotcha
update returns None, not the dict — do not chain (e.g. d.update(...).items() fails). Later sources win: kwargs override the positional mapping.
Related methods
dict1 | dict2
Merge operator (Python 3.9+) that returns a new dict containing the entries of d1 followed by d2, with d2's values winning on shared keys. Both operands must be dicts; neither is mutated.
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.
dict.setdefault
If key is in the dictionary, returns its value. If not, inserts key with a value of default (None if omitted) and returns default.
dict.pop
Removes the specified key and returns its value. If the key is missing, returns default if provided; otherwise raises KeyError.
dict.popitem
Removes and returns the last inserted (key, value) pair as a 2-tuple in LIFO order. Raises KeyError if the dictionary is empty.
← All Python dict methods · String methods · Built-in functions