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.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
