To update a value in a Python dictionary, assign to the key with d[key] = value. To change several entries at once, call d.update(other) or, from Python 3.9, merge with the |= operator. If the key does not exist, assignment adds it.
Dictionaries are mutable and keep insertion order, so these operations change the same object in place. The sections below cover each approach, when to prefer it and how to avoid common pitfalls.
update() and |=setdefault() and get()Assigning to a key replaces its value if the key exists and creates the entry if it does not.
Because assignment also creates keys, a typo in the key name silently adds a new entry. Check with if key in person: when you want to update only existing keys.
update() and |=dict.update() accepts another dictionary, an iterable of key-value pairs or keyword arguments. Existing keys are overwritten and new keys are added.
The | operator (Python 3.9 and later) builds a new merged dictionary, while |= updates the left side in place. In both cases the right-hand value wins when keys collide.
A dictionary comprehension builds a new dictionary and leaves the original unchanged, which is useful for transformations.
To change values in place while looping, assign by key. Changing values during iteration is safe, but adding or removing keys while iterating raises RuntimeError.
setdefault() and get()setdefault(key, default) returns the existing value if the key is present. Otherwise it inserts the default and returns it. It never overwrites an existing value, so it does not update existing keys.
For counters and grouped lists, collections.defaultdict and collections.Counter are cleaner. You can also combine get() with a default to increment safely.
update() is shallow: it replaces a nested dictionary as a whole instead of merging it.
For deep merging, write a small recursive function or use a library. Use copy.deepcopy() when you need an independent copy of nested data first.
| Method | Changes original | Adds missing keys | Overwrites existing |
|---|---|---|---|
d[key] = value | Yes | Yes | Yes |
d.update(other) | Yes | Yes | Yes |
d |= other | Yes | Yes | Yes |
d | other | No, returns new dict | Yes | Yes |
setdefault() | Only if key is missing | Yes | No |
| Comprehension | No, returns new dict | Depends | Yes |
Test membership first: if key in d: d[key] = value. Alternatively filter the update with a comprehension over the keys you want to allow.
update() and assignment?Assignment sets one key. update() sets many keys in one call from a dictionary, pairs or keyword arguments.
update() return a new dictionary?No. It modifies the dictionary in place and returns None. Use d1 | d2 for a new merged dictionary.
Index down to the inner dictionary and assign, for example config["db"]["port"] = 5433. Do not rely on update() for nested merges.
Use d[key] = value for single changes, update() or |= for bulk changes, comprehensions to build transformed copies and setdefault() only to fill in missing keys. Remember that update() is shallow and returns None.