How to Update Values in a Python Dictionary

Assign to the key. That’s the whole trick to update values in a Python dictionary: d[key] = new_value replaces the old value, and d.update() changes several keys in one call:

prices["coffee"] = 3.75
prices.update({"bagel": 2.55, "muffin": 2.66})

A small coffee-shop price list runs through every example, and each output was captured on Python 3.12.5.

In this tutorial, we’ll change one value, several values, only existing keys, every value at once, and values inside nested dictionaries.

Change one dictionary value with square-bracket assignment

Name the key on the left and the new value on the right. Augmented operators like += read the current value first, so price changes stay one line:

prices = {"coffee": 3.50, "bagel": 2.25, "muffin": 2.95}

prices["coffee"] = 3.75          # replace a value
prices["bagel"] += 0.30          # change it based on the old value
prices["muffin"] = round(prices["muffin"] * 0.9, 2)   # 10% off

for item, price in prices.items():
    print(f"{item:<7} ${price:.2f}")

Output:

coffee  $3.75
bagel   $2.55
muffin  $2.66
Command Prompt output of Python updating three dictionary prices with assignment, += and a 10% discount
Coffee was replaced outright, the bagel went up 30 cents and the muffin got 10% off.

Counters work the same way; incrementing a value in a Python dictionary has more patterns for tallies.

Update several values at once with update()

update() accepts another dictionary or keyword arguments. Be careful, though: any key it doesn’t find gets added, not rejected:

stock = {"coffee": 40, "bagel": 25, "muffin": 18}

stock.update({"coffee": 36, "bagel": 20})   # several keys in one call
stock.update(muffin=12)                     # keyword form
print(stock)

stock.update(scone=8)                       # a missing key gets ADDED
print(stock)

Output:

{'coffee': 36, 'bagel': 20, 'muffin': 12}
{'coffee': 36, 'bagel': 20, 'muffin': 12, 'scone': 8}

That silent insert is why I don’t use update() for corrections to known fields. Merging with | and |= is covered in my Python dictionary update() guide.

How do I update a value only if the key exists?

Check with in before assigning. For counters, get(key, 0) supplies a starting value. Without either, += on a missing key raises KeyError:

stock = {"coffee": 36, "bagel": 20}

def set_existing(d, key, value):
    if key in d:
        d[key] = value
        return True
    return False

print(set_existing(stock, "bagel", 15), stock)
print(set_existing(stock, "donut", 10), stock)

sales = {}
for item in ["coffee", "bagel", "coffee"]:
    sales[item] = sales.get(item, 0) + 1   # safe even when the key is new
print(sales)

try:
    stock["donut"] += 1
except KeyError as err:
    print("KeyError:", err)

Output:

True {'coffee': 36, 'bagel': 15}
False {'coffee': 36, 'bagel': 15}
{'coffee': 2, 'bagel': 1}
KeyError: 'donut'
Command Prompt output of a Python helper updating a dictionary only when the key exists, get() counting sales and a KeyError
The helper returns True for bagel and False for donut, leaving the dictionary untouched the second time.

The full set of options for missing keys is in handling KeyError with try-except in a Python dictionary.

Update every value in a dictionary with a loop or comprehension

A comprehension builds a fresh dictionary, which suits a blanket change like an 8% price rise. A loop edits the original and handles conditions:

prices = {"coffee": 3.75, "bagel": 2.55, "muffin": 2.66}

# comprehension: builds a NEW dict with every price up 8%
raised = {item: round(p * 1.08, 2) for item, p in prices.items()}
print(raised)

# loop: changes the SAME dict in place, only for cheap items
for item in prices:
    if prices[item] < 3:
        prices[item] = 3.00
print(prices)

Output:

{'coffee': 4.05, 'bagel': 2.75, 'muffin': 2.87}
{'coffee': 3.75, 'bagel': 3.0, 'muffin': 3.0}
Command Prompt output of a Python dict comprehension raising all prices 8% and a loop setting a minimum price
The comprehension prints new rounded prices; the loop lifts the bagel and muffin to 3.0 and leaves coffee alone.

Changing values while looping is safe; adding or deleting keys isn’t. More on that in iterating through a Python dictionary, and Python dictionary comprehension goes deeper.

Update values in a nested dictionary

Chain the keys to reach the inner value. A list value can be changed in place with its own methods, with no reassignment needed:

orders = {
    "A-1001": {"customer": "Emily", "status": "pending", "items": ["latte"]},
    "A-1002": {"customer": "James", "status": "pending", "items": ["bagel"]},
}

orders["A-1001"]["status"] = "shipped"        # inner value
orders["A-1002"]["items"].append("muffin")     # list value changed in place
orders["A-1002"].setdefault("shipped_on", "2026-10-18")

for oid, info in orders.items():
    print(oid, info)

Output:

A-1001 {'customer': 'Emily', 'status': 'shipped', 'items': ['latte']}
A-1002 {'customer': 'James', 'status': 'pending', 'items': ['bagel', 'muffin'], 'shipped_on': '2026-10-18'}
Command Prompt output of Python updating a status and appending to a list inside a nested dictionary of orders
Order A-1001 is now shipped, and A-1002 has a muffin added plus a shipped_on date.

Reading deep values that might be missing? See KeyError in a nested Python dictionary.

Keep going with these dictionary tutorials:

Frequently asked questions

How do I change a value in a Python dictionary?

Assign to its key: d["coffee"] = 3.75.

Does update() add new keys?

Yes. Keys that don’t exist are added. Check with in first if you only want existing keys changed.

How do I update all values in a dictionary?

Use a comprehension, {k: f(v) for k, v in d.items()}, or a loop that assigns d[k].

Why do I get a KeyError when I use +=?

+= reads the old value first. Use d[k] = d.get(k, 0) + 1 instead.

Can I change values while looping over a dictionary?

Yes, changing values is fine. Adding or removing keys during the loop raises a RuntimeError.

Where are dictionary methods documented?

See dict in the Python docs.