Python Dictionary Update: 5 Ways With Examples

If you’ve tried a Python dictionary update like prices = prices.update(feed), you’ve probably seen your whole dictionary turn into None.

That’s because dict.update() changes the dictionary in place and returns nothing. Call it on its own line: prices.update(feed).

In this tutorial, we’ll do a python dictionary update five ways on one price list, from update() to |= and nested data. I ran it all on Python 3.12.5.

The sample price list for our Python dictionary update

A store keeps prices by SKU, and a vendor feed sends changes:

prices = {"SKU-101": 19.99, "SKU-102": 4.50, "SKU-103": 129.00}
feed = {"SKU-102": 4.75, "SKU-104": 59.99}

SKU-102 should change to $4.75 and SKU-104 is new. New to dictionaries? Start with how Python dictionaries work.

Here’s that mistake on the sample:

from inventory import prices, feed

prices = prices.update(feed)
print(prices)
print(prices["SKU-102"])

Output:

None
Traceback (most recent call last):
  File "C:\pyguides\assign_update.py", line 5, in <module>
    print(prices["SKU-102"])
          ~~~~~~^^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable
Command Prompt output showing Python dict update returning None and a TypeError on the next lookup
Assigning the result of update() prints None, and the next lookup fails with a TypeError.

Method 1: Update a dictionary with dict.update() (Recommended)

Use update() when another dictionary holds the new values. Same price list and feed:

from inventory import prices, feed

print("Before:", prices)
prices.update(feed)
print("After: ", prices)
print(f"{len(prices)} SKUs, SKU-102 now ${prices['SKU-102']:.2f}")

Output:

Before: {'SKU-101': 19.99, 'SKU-102': 4.5, 'SKU-103': 129.0}
After:  {'SKU-101': 19.99, 'SKU-102': 4.75, 'SKU-103': 129.0, 'SKU-104': 59.99}
4 SKUs, SKU-102 now $4.75
Command Prompt output of Python dictionary update() changing SKU-102 and adding SKU-104
update() raises SKU-102 to $4.75 and adds SKU-104, so 3 SKUs become 4.

How does this code work?

For each key in feed, Python sets prices[key] = value, overwriting existing keys and adding missing ones at the end.

That’s why the method is also the answer to “how do I extend a dictionary”. To change single values instead, see updating values in a Python dictionary.

How to update a dictionary from tuples, zip() or keyword arguments

update() also takes any iterable of key-value pairs, plus keyword arguments:

from inventory import prices, feed

rows = [("SKU-101", 18.49), ("SKU-105", 7.25)]
prices.update(rows)
prices.update(zip(["SKU-103"], [119.00]))
print(prices)

settings = {"store": "Austin, TX", "tax_rate": 0.0625}
settings.update(tax_rate=0.0825, currency="USD")
print(settings)

Output:

{'SKU-101': 18.49, 'SKU-102': 4.5, 'SKU-103': 119.0, 'SKU-105': 7.25}
{'store': 'Austin, TX', 'tax_rate': 0.0825, 'currency': 'USD'}

SKU-101 drops to $18.49, SKU-103 to $119.00, SKU-105 is added, and the tax rate becomes 8.25%.

How does this code work?

Each tuple must be exactly one key and one value. The zip() function builds those pairs from two lists.

Important: keyword arguments need keys that are valid Python names; SKU-101=18.49 is a syntax error.

Method 2: Use the | and |= dictionary merge operators (Python 3.9+)

Python 3.9 added |, which builds a new dictionary, and |=, which updates in place. Same price list:

from inventory import prices, feed

merged = prices | feed
print("New dict:", merged)
print("Original:", prices)

prices |= {"SKU-101": 17.99}
print("After |=:", prices)

Output:

New dict: {'SKU-101': 19.99, 'SKU-102': 4.75, 'SKU-103': 129.0, 'SKU-104': 59.99}
Original: {'SKU-101': 19.99, 'SKU-102': 4.5, 'SKU-103': 129.0}
After |=: {'SKU-101': 17.99, 'SKU-102': 4.5, 'SKU-103': 129.0}
Command Prompt output of the Python dict merge operator | and the in-place |= update
prices | feed returns a new four-SKU dict while the original keeps three; |= then changes SKU-101 to $17.99.

How does this code work?

The right side wins on duplicate keys, just like update().

More ways to join two dictionaries: concatenating dictionaries in Python.

Method 3: Update only existing keys, or only add missing ones

Sometimes the feed mustn’t add SKUs, or defaults mustn’t overwrite real prices. Same sample data, filtered first:

from inventory import prices, feed

known = {k: v for k, v in feed.items() if k in prices}
prices.update(known)
print("Existing keys only:", prices)

catalog = {"SKU-101": 0.00, "SKU-106": 12.00}
for sku, price in catalog.items():
    prices.setdefault(sku, price)
print("Missing keys only: ", prices)

Output:

Existing keys only: {'SKU-101': 19.99, 'SKU-102': 4.75, 'SKU-103': 129.0}
Missing keys only:  {'SKU-101': 19.99, 'SKU-102': 4.75, 'SKU-103': 129.0, 'SKU-106': 12.0}

Pass one changes only SKU-102. Pass two adds SKU-106 at $12.00 and keeps SKU-101 at $19.99.

How does this code work?

The dictionary comprehension keeps only feed keys already in prices.

setdefault() writes only when the key is missing, a safe way to extend a dict. For adding keys in general, see Python dictionary append.

Why does update() replace a nested dictionary?

update() is shallow, so an inner dictionary gets replaced, not merged:

store = {"TX": {"tax": 0.0625, "open": True}}
store.update({"TX": {"tax": 0.0825}})
print("update():  ", store)

def deep_update(target, changes):
    for key, value in changes.items():
        if isinstance(value, dict) and isinstance(target.get(key), dict):
            deep_update(target[key], value)
        else:
            target[key] = value

store = {"TX": {"tax": 0.0625, "open": True}}
deep_update(store, {"TX": {"tax": 0.0825}})
print("deep merge:", store)

Output:

update():   {'TX': {'tax': 0.0825}}
deep merge: {'TX': {'tax': 0.0825, 'open': True}}
Command Prompt output comparing a shallow Python dict update with a deep merge of a nested dictionary
Plain update() drops the open flag; the recursive deep_update() keeps it and changes only the tax.

How does this code work?

deep_update() recurses when both sides hold a dictionary and assigns otherwise.

Which Python dictionary update method should you use?

SituationUseWhy
Change or add from another dictd.update(other)Any Python 3
Pairs from a CSV or zip()d.update(pairs)No dict needed
Keep the originalnew = d | otherNew dict (3.9+)
Only existing / only missing keysComprehension / setdefault()Picks the keys
Nested dictionariesRecursive helperupdate() is shallow

I default to update(), and | when the original must survive.

Things to keep in mind when you update a dictionary

  • Don’t assign it. update() returns None.
  • Don’t update while looping over the same dict. Adding keys mid-loop raises RuntimeError; iterate over a copy.
  • Watch key spelling. A typo silently adds a key; later lookups raise a dictionary KeyError.

Keep going with these dictionary guides:

Frequently asked questions

What does dict.update() return in Python?

None. It changes the dictionary in place, so don’t assign the result.

Does update() overwrite existing keys?

Yes. Existing keys get the new value; new keys are added.

How do I extend a dictionary with another dictionary?

Use d.update(other), or d |= other on Python 3.9+. For missing keys only, loop with setdefault().

What’s the difference between update() and the | operator?

update() and |= change the dictionary in place. | returns a new dictionary and leaves both originals alone.

Why do I get “dictionary update sequence element #0 has length 3”?

Each item passed to update() must be a pair. A tuple or list with three items raises this ValueError.

Where is dict.update() documented?

See dict.update() in the official Python docs.