Python Tutoring Online
 Home

Change Dictionary Items in Python

Python dictionaries are mutable, which means the values they hold can be modified at any time after the dictionary is created. Changing a value doesn't touch the dictionary's other keys or its structure — only the specific value you target gets replaced.

This guide covers how to change a single value, update several at once, change values based on their current value, and safely modify values without risking an accidental key addition.

Need Help Learning Python?

If you are learning Python and finding programming concepts difficult, working with an experienced Python tutor can help you progress faster.

One-to-one Python tutoring sessions provide practical coding exercises, clear explanations and help with assignments, projects and debugging.

Get Python Tutoring Help

Syntax

To change a dictionary value, reference an existing key and assign it a new value with the assignment operator.

Python
dictionary[key] = new_value

Python finds key and replaces its current value with new_value. (If the key doesn't already exist, this same syntax inserts it as a new item instead — see Add Dictionary Items if that's what you're trying to do.)

Example 1: Change a Single Value

Python
product = {
"name": "Wireless Mouse",
"price": 25,
"in_stock": True
}

product["price"] = 20

print(product)

Output


{'name': 'Wireless Mouse', 'price': 20, 'in_stock': True}

The key "price" already existed, so its value is replaced — the dictionary still has the same three keys, just with an updated price.

Example 2: Change Multiple Values

Python
product = {
"name": "Wireless Mouse",
"price": 25,
"in_stock": True
}

product["price"] = 22
product["in_stock"] = False

print(product)

Output


{'name': 'Wireless Mouse', 'price': 22, 'in_stock': False}

Each assignment updates a different key independently — the dictionary's size and key names stay exactly the same.

Changing a Value Based on Its Current Value

A very common update pattern is calculating a new value from the existing one — like applying a discount, a raise, or a percentage adjustment — rather than replacing it with a fixed value.

Python
product = {
"name": "Wireless Mouse",
"price": 25
}

# apply a 10% discount to the current price
product["price"] = product["price"] * 0.9

print(product)

Output


{'name': 'Wireless Mouse', 'price': 22.5}

The right side of the assignment reads the current value first, then the result overwrites it — this is the pattern behind most "update this value" logic you'll write in real programs.

Updating Multiple Values at Once With update()

When you need to change several existing values together, update() lets you apply them all in a single call instead of writing one assignment line per key.

Python
product = {
"name": "Wireless Mouse",
"price": 25,
"in_stock": True
}

product.update({
"price": 19,
"in_stock": False
})

print(product)

Output


{'name': 'Wireless Mouse', 'price': 19, 'in_stock': False}

Both price and in_stock are changed in one step. Note that if any key you pass to update() doesn't already exist, it gets added rather than raising an error — update() doesn't distinguish between changing and adding.

Changing Values Conditionally

Often you only want to update a value if it meets some condition — for example, only reducing a price if it's currently above a certain threshold.

Python
prices = {
"mouse": 25,
"keyboard": 45,
"monitor": 15
}

for item in prices:
    if prices[item] > 20:
        prices[item] = prices[item] - 5

print(prices)

Output


{'mouse': 20, 'keyboard': 40, 'monitor': 15}

Only items priced above 20 get the discount applied — monitor is left unchanged because it didn't meet the condition. This is a common pattern when applying business rules to existing dictionary data.

Changing Values in Nested Dictionaries

To change a value inside a nested dictionary, chain the keys together and assign to the innermost one — the outer structure stays untouched.

Python
inventory = {
"mouse": {"price": 25, "stock": 40},
"keyboard": {"price": 45, "stock": 15}
}

inventory["mouse"]["stock"] = 35

print(inventory)

Output


{'mouse': {'price': 25, 'stock': 35}, 'keyboard': {'price': 45, 'stock': 15}}

Only mouse's stock value changes — the keyboard entry and mouse's price are unaffected. See Nested Dictionaries for more on working with nested structures.

Updating Every Value With a Loop

To apply the same kind of change across an entire dictionary — like giving every student a bonus, or converting every price to a different currency — loop through the keys and update each value in place.

Python
scores = {
"Alice": 80,
"Bob": 85,
"Charlie": 90
}

for student in scores:
    scores[student] = scores[student] + 5

print(scores)

Output


{'Alice': 85, 'Bob': 90, 'Charlie': 95}

Every existing value increases by 5 — no keys are added or removed, only the values already present are changed.

Safely Changing a Value Only If the Key Exists

Directly assigning to a key that doesn't exist will silently create it — which isn't always what you want if your intent is strictly to update. Check for the key first with the in operator if you want to guarantee you're only ever changing, never adding.

Python
product = {
"name": "Wireless Mouse",
"price": 25
}

key_to_update = "warranty"

if key_to_update in product:
    product[key_to_update] = "2 years"
else:
    print(f"'{key_to_update}' does not exist — no update made")

print(product)

Output


'warranty' does not exist — no update made
{'name': 'Wireless Mouse', 'price': 25}

This guards against typos in key names silently creating unwanted new entries instead of updating the one you meant. See Check Key in Dictionary for more on the in operator.

Real-World Use Case: Applying a Price Increase Across a Catalog

A typical real-world update task is applying a percentage change to every value in a dictionary — for example, raising all product prices by a fixed percentage during an inflation adjustment.

Python
catalog = {
"mouse": 25,
"keyboard": 45,
"monitor": 150
}

increase_percent = 8

for item in catalog:
    catalog[item] = round(catalog[item] * (1 + increase_percent / 100), 2)

print(catalog)

Output


{'mouse': 27.0, 'keyboard': 48.6, 'monitor': 162.0}

Every price is recalculated from its own current value — no items are added or removed, and each key keeps referring to the same product, just with an updated price.

Common Beginner Mistakes

Frequently Asked Questions

How do I change a value in a Python dictionary?

Assign a new value to an existing key: dictionary[key] = new_value. Python replaces the current value without affecting any other keys.

How do I update a value based on what it currently is?

Read the current value on the right side of the assignment before overwriting it, for example dictionary[key] = dictionary[key] + 1.

What happens if I try to change a key that doesn't exist?

Python creates it as a new key instead of raising an error. If you need to guarantee you're only updating, check first with the in operator.

What's the difference between changing and adding a dictionary item?

Changing modifies the value of a key that's already there; adding inserts a brand-new key. Python uses the same dictionary[key] = value syntax for both — see Add Dictionary Items for insertion-focused examples.

Related Python Dictionary Tutorials




Stuck Learning Python?

Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.

Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net