Start learning free

LESSON 124 OF 673 · DICTIONARIES

Accessing Dictionary Values in Python

Lesson

Once you have a dictionary, you can access any value by its key using square bracket notation:

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

alice_score = scores["Alice"]  # 95
bob_score = scores["Bob"]      # 87

If the key does not exist, you get a KeyError. Use get() to avoid this:

score = scores.get("Diana", 0)  # returns 0 if "Diana" not found

You can also check if a key exists with in:

if "Alice" in scores:
    print(scores["Alice"])

Assignment
Follow the Coding Tutorial and access Alice's score from scores!


Hint
Use scores["Alice"] and assign it to alice_score.

STEP 1 OF 1
    Ctrl + Enter to check