Dictionary Methods
update()
Update the dictionary with key-value pairs from another dict or iterable.
dict.update([other], **kwargs)
dict.update() merges keys and values from other (or keyword arguments) into the dictionary, overwriting existing keys.
It accepts another dictionary, an iterable of (key, value) pairs, or keyword arguments.
Parameters
| Name | Description |
|---|---|
| other | Optional dictionary or iterable of key-value pairs to merge. |
| **kwargs | Optional keyword arguments where parameter names become keys. |
Returns
None — the dictionary is updated in place.
Try it
Run it and change it
defaults = {"theme": "light", "fontSize": 12, "zoom": 100}
user_prefs = {"theme": "dark", "zoom": 120}
defaults.update(user_prefs)
defaults.update(language="en_US")
print(defaults)Worth knowing
- Dictionary unpacking alternative: In Python 3.9+, you can also merge dictionaries using the union operator:
merged = dict1 | dict2without modifying either in place.