|
| Home |
|---|
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.
To change a dictionary value, reference an existing key and assign it a new value with the assignment operator.
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.)
product = {
"name": "Wireless Mouse",
"price": 25,
"in_stock": True
}
product["price"] = 20
print(product)
{'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.
product = {
"name": "Wireless Mouse",
"price": 25,
"in_stock": True
}
product["price"] = 22
product["in_stock"] = False
print(product)
{'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.
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.
product = {
"name": "Wireless Mouse",
"price": 25
}
# apply a 10% discount to the current price
product["price"] = product["price"] * 0.9
print(product)
{'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.
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.
product = {
"name": "Wireless Mouse",
"price": 25,
"in_stock": True
}
product.update({
"price": 19,
"in_stock": False
})
print(product)
{'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.
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.
prices = {
"mouse": 25,
"keyboard": 45,
"monitor": 15
}
for item in prices:
if prices[item] > 20:
prices[item] = prices[item] - 5
print(prices)
{'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.
To change a value inside a nested dictionary, chain the keys together and assign to the innermost one — the outer structure stays untouched.
inventory = {
"mouse": {"price": 25, "stock": 40},
"keyboard": {"price": 45, "stock": 15}
}
inventory["mouse"]["stock"] = 35
print(inventory)
{'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.
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.
scores = {
"Alice": 80,
"Bob": 85,
"Charlie": 90
}
for student in scores:
scores[student] = scores[student] + 5
print(scores)
{'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.
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.
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)
'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.
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.
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)
{'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.
x = 5 instead of x = x + 5)
Assign a new value to an existing key: dictionary[key] = new_value.
Python replaces the current value without affecting any other keys.
Read the current value on the right side of the assignment before
overwriting it, for example dictionary[key] = dictionary[key] + 1.
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.
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.
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.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net