Python dict update() Method Explained

How-to basics · Updated November 23, 2026

The dict.update() method in Python merges keys and values from another dictionary, an iterable of key-value pairs, or keyword arguments into the existing dictionary, overwriting any keys that already exist. For example, d.update({'a': 1}) adds or replaces the key 'a' in d with value 1. It modifies the dictionary in place and returns None.

What update() Does and Its Signature

update() is a built-in method of Python’s dict class. Its formal signature is update([other]), where other can be:

  • Another dictionary
  • An iterable of key-value pairs (e.g., a list of tuples)
  • Keyword arguments (d.update(a=1, b=2))

When called, update() iterates over the provided items and assigns each key to the given value in the target dictionary. If a key already exists, its value is replaced; if not, a new key is added. The operation is atomic in the sense that it either completes or raises an exception, but partial updates can occur if an error happens mid-iteration (e.g., if the iterable yields an invalid pair).

How to Use update() with Different Input Types

The most common usage is merging another dictionary:

d = {'a': 1, 'b': 2}
d.update({'b': 3, 'c': 4})
print(d)  # {'a': 1, 'b': 3, 'c': 4}

You can also pass a list of tuples:

d = {}
d.update([('x', 10), ('y', 20)])
print(d)  # {'x': 10, 'y': 20}

And keyword arguments work as well:

d = {}
d.update(name='Alice', age=30)
print(d)  # {'name': 'Alice', 'age': 30}

Note that keyword arguments only work when the keys are valid Python identifiers (strings that are not reserved words). For keys with spaces or special characters, use a dictionary or iterable of pairs.

Return Value and In-Place Modification

Unlike many Python methods that return a new object, update() modifies the original dictionary in place and returns None. This is a common source of confusion: beginners often expect d = d.update(other) to work, but that sets d to None. The correct pattern is simply d.update(other).

This in-place behavior is efficient because it avoids creating a copy of the entire dictionary. However, if you need a new dictionary without modifying the original, use the {**d1, **d2} unpacking syntax or the | operator (Python 3.9+).

Common Use Cases and Examples

Here are typical scenarios where update() shines:

  • Merging configuration defaults with user overrides – start with a defaults dict, then update with user settings.
  • Accumulating counts – update a counter dict with new occurrences.
  • Building a dictionary from a list of pairs – often used with zip().
defaults = {'theme': 'light', 'font': 'Arial'}
user_prefs = {'font': 'Helvetica', 'fontsize': 12}
defaults.update(user_prefs)
print(defaults)  # {'theme': 'light', 'font': 'Helvetica', 'fontsize': 12}

Another example using zip() to create a dict from two lists:

keys = ['name', 'age']
values = ['Bob', 25]
d = {}
d.update(zip(keys, values))
print(d)  # {'name': 'Bob', 'age': 25}

update() vs. Alternatives (|, {**dict}, and dict() constructor)

Python offers several ways to merge dictionaries, each with different semantics:

  • d1.update(d2) – modifies d1 in place, returns None.
  • merged = {**d1, **d2} – creates a new dict, does not modify originals. Works in Python 3.5+.
  • merged = d1 | d2 – Python 3.9+ union operator, also creates a new dict.
  • dict(d1, **d2) – works but only for string keys in d2 (keyword args).

Choose update() when you want to mutate an existing dict, especially in loops or when building up a result. Choose the unpacking or | operator when you need a new dict and want to keep the originals unchanged. The | operator is also more readable in many cases.

Common Pitfalls and Misconceptions

One frequent mistake is assuming update() returns the updated dictionary. It does not; it returns None. Another pitfall is trying to update with a non-iterable object, which raises TypeError: 'int' object is not iterable. Also, if you pass an iterable that yields pairs of the wrong length (e.g., a list of single items), you’ll get a ValueError.

Another misconception is that update() performs a deep merge. It does not – it only replaces values at the top level. If you have nested dictionaries, the inner dicts are replaced entirely, not merged recursively. For deep merging, you need a custom function or a library like collections.ChainMap (though that doesn’t modify the original).

Finally, be aware that update() can accept keyword arguments along with another argument, but if you pass both, the keyword arguments are processed after the positional one, so they will override any same-named keys from the positional source.

Performance and Efficiency Considerations

In CPython, update() is implemented in C and is generally fast. It iterates over the source and inserts each key-value pair, which is O(n) in the size of the source. For merging two large dictionaries, this is efficient. However, if you are repeatedly updating a dictionary inside a tight loop, consider whether you can build the dict in one go using a comprehension or the dict() constructor, as that may be more readable and slightly faster.

Memory-wise, update() does not create a copy of the source; it just references the same key and value objects. This is usually fine, but if you need to avoid retaining references to large objects, you may want to copy values explicitly.

Frequently Asked Questions

Does dict.update() return a new dictionary? No, it modifies the dictionary in place and returns None. If you need a new dictionary, use the | operator or {**d1, **d2}.

Can I use update() with keyword arguments? Yes, d.update(a=1, b=2) works, but keys must be valid Python identifiers. For keys with spaces or special characters, use a dictionary or list of pairs.

What happens if a key exists in both dictionaries? The value from the argument to update() overwrites the existing one. The original key’s value is replaced, not merged.

Is there a way to merge dictionaries without modifying the original? Yes, use merged = {**d1, **d2} (Python 3.5+) or merged = d1 | d2 (Python 3.9+). Both create a new dictionary.

Why does d.update(other) not assign to d? Because update() returns None. The assignment would set d to None. Just call d.update(other) without assignment.

Can update() be used with a list of tuples? Yes, any iterable of key-value pairs works, including a list of two-element tuples or a zip object.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.