Start learning free

LESSON 125 OF 673 · DICTIONARIES

Updating Dictionary Values in Python

Lesson

You can update an existing value in a Python dictionary by assigning a new value to an existing key:

scores = {"Alice": 95, "Bob": 87}

scores["Alice"] = 99    # update existing key
scores["Charlie"] = 72  # add new key

If the key exists, the value is overwritten. If it doesn't exist, a new entry is added.

d = {"x": 1}
d["x"] = 100   # x is now 100
d["y"] = 200   # new key "y" added
print(d)       # {"x": 100, "y": 200}

Assignment
Follow the Coding Tutorial and update Alice's score to 99!


Hint
Use scores["Alice"] = 99.

STEP 1 OF 1
    Ctrl + Enter to check