W3docs

Python Dictionaries: How to Change Key and Value

Learn how to change dictionary values, rename keys, and bulk-update items in Python using assignment, pop(), and update() with clear examples.

Python dictionaries let you store data as key-value pairs and update them freely after creation. This chapter covers every practical way to modify existing dictionary items: changing a single value, renaming a key, bulk-updating with update(), and handling nested dictionaries — including the edge cases you will encounter in real code.

For background on how dictionaries work, see the Accessing Dictionary Items chapter. To add brand-new entries, see Add Dictionary Items. To delete entries, see Remove Dictionary Items.

Changing a Value

The simplest modification is reassigning the value stored under an existing key. Use the key as the index on the left-hand side of an assignment:

python— editable, runs on the server

If the key already exists, the value is overwritten in place. The other entries are not affected and the dictionary preserves its insertion order (Python 3.7+).

What happens when the key does not exist?

If you use assignment with a key that is not already in the dictionary, Python silently creates a new entry instead of raising an error:

prices = {'apple': 1.00, 'banana': 0.50}
prices['mango'] = 2.50   # 'mango' did not exist — a new entry is created
print(prices)
# {'apple': 1.00, 'banana': 0.50, 'mango': 2.50}

When you want to update only if a key exists, check first with in:

if 'banana' in prices:
    prices['banana'] = 0.75

Bulk-Updating with update()

dict.update() lets you change several values at once. Pass it a dictionary (or keyword arguments) of key-value pairs to apply:

inventory = {'apple': 10, 'banana': 5, 'orange': 8}
inventory.update({'banana': 12, 'orange': 3})
print(inventory)
# {'apple': 10, 'banana': 12, 'orange': 3}

Keys present in the argument overwrite the existing values; keys not mentioned are left unchanged. You can also pass keyword arguments directly:

inventory.update(apple=20, banana=15)
print(inventory)
# {'apple': 20, 'banana': 15, 'orange': 3}

update() also adds any keys from the argument that do not yet exist in the target dictionary, making it useful for merging two dictionaries together.

Renaming (Changing) a Key

Dictionary keys are immutable references — you cannot rename a key directly. The standard pattern is to add a new key with the old value and delete the old key. The pop() method does both in one expression:

python— editable, runs on the server

pop('banana') returns 2 and removes the 'banana' entry. Assigning that return value to my_dict['pear'] creates the renamed entry.

Note that the renamed key appears at the end of the dictionary, not in the original position of 'banana', because it is a new key being inserted.

Renaming a key when you are unsure it exists

If the key might be absent, pop() raises a KeyError. Pass a default as the second argument to avoid the exception:

value = my_dict.pop('grape', None)  # returns None if 'grape' is missing
if value is not None:
    my_dict['kiwi'] = value

Changing Both the Key and the Value

Combine the rename pattern with a value reassignment to change both at once:

python— editable, runs on the server

Or collapse it into a single step by directly assigning the desired value instead of the popped value:

my_dict = {'apple': 1, 'banana': 2, 'orange': 3}
my_dict.pop('banana')          # remove old key (discard value)
my_dict['pear'] = 4            # insert new key with new value
print(my_dict)
# {'apple': 1, 'orange': 3, 'pear': 4}

Updating Items in a Nested Dictionary

When a dictionary contains other dictionaries as values, use chained indexing to reach the inner value:

catalog = {
    'apple':  {'price': 1.00, 'stock': 50},
    'banana': {'price': 0.50, 'stock': 30},
}

# Update only the price of 'banana'
catalog['banana']['price'] = 0.75
print(catalog['banana'])
# {'price': 0.75, 'stock': 30}

You can also call update() on the inner dictionary:

catalog['apple'].update({'price': 1.20, 'stock': 45})
print(catalog['apple'])
# {'price': 1.20, 'stock': 45}

Common Gotchas

SituationWhat happensFix
Assigning to a missing keyCreates a new entry (no error)Check if key in d first when you only want to update
Calling pop() on a missing keyRaises KeyErrorUse d.pop(key, default)
Renaming keeps original positionNew key goes to the endIf order matters, rebuild with a dict comprehension
update() with overlapping keysOverwrites silentlyIntentional — that is the purpose of update()

Rebuilding to preserve key order

If you need the renamed key to stay in its original position, rebuild the dictionary with a comprehension:

my_dict = {'apple': 1, 'banana': 2, 'orange': 3}
my_dict = {
    ('pear' if k == 'banana' else k): v
    for k, v in my_dict.items()
}
print(my_dict)
# {'apple': 1, 'pear': 2, 'orange': 3}

This iterates over every existing entry and emits the renamed key only where the condition is true, leaving everything else unchanged.

Practice

Practice
In Python, how can you change the items in a list?
In Python, how can you change the items in a list?
Was this page helpful?