dict.update()
Merges another mapping or key/value pairs into a dict in-place.
data.update(other)
data.update(key=value)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Merges another mapping or key/value pairs into a dict in-place.
The dict.update() method merges a dictionary with another mapping or an iterable of key-value pairs (such as a list of tuples). It performs an in-place mutation, meaning it modifies the original object and returns None. If keyword arguments are provided, they are processed after the first argument, allowing for specific overrides. Performance is typically O(K), where K is the number of elements in the incoming data, as it must iterate and hash each key. A critical edge case involves key collisions: if a key exists in both the target and the source, the target's value is overwritten by the source's value. This method is preferred over manual looping for efficiency and readability when combining configuration objects or applying default values.
Quick reference
Syntax
data.update(other)
data.update(key=value)
Inputs
Parameters
See it in practice
Examples
Merging Two Dictionaries
settings = {'theme': 'light', 'notify': True}
extra_config = {'theme': 'dark', 'version': 2.0}
settings.update(extra_config)
print(settings){'theme': 'dark', 'notify': True, 'version': 2.0}
Existing keys like 'theme' are overwritten, while new keys like 'version' are added.
Updating with Keyword Arguments
profile = {'user': 'admin'}
profile.update(status='active', attempts=3)
print(profile){'user': 'admin', 'status': 'active', 'attempts': 3}
Keyword arguments allow adding or updating specific keys directly without creating a temporary dictionary.
Using an Iterable of Tuples
counts = {'a': 1}
updates = [('b', 2), ('c', 3)]
counts.update(updates)
print(counts){'a': 1, 'b': 2, 'c': 3}
The method accepts any iterable containing pairs of items, making it compatible with list of tuples or generated sequences.
Debug faster
Common Errors
LogicError
Cause: Assuming update() returns the modified dictionary for chaining or assignment.
Fix: Call update() on a separate line; do not assign its result to a variable.
data = {'x': 1}
new_data = data.update({'y': 2}) # new_data is now NoneTypeError
Cause: Passing multiple dictionaries as positional arguments or passing an incompatible iterable.
Fix: Pass a single dictionary/iterable as the first argument, or use keyword arguments.
d = {}
d.update({'a': 1}, {'b': 2}) # Raises TypeErrorRuntime support
Compatibility
Source: Python Software Foundation
Common questions
Frequently Asked Questions
Merges another mapping or key/value pairs into a dict in-place.
other: Dict or iterable of (key, value) pairs. **kwargs: Extra key=value pairs to add/overwrite.
LogicError: Call update() on a separate line; do not assign its result to a variable. TypeError: Pass a single dictionary/iterable as the first argument, or use keyword arguments.