In Python, you can get the number of key-value pairs in a dictionary using len():
scores = {"Alice": 95, "Bob": 87, "Charlie": 72}
n = len(scores)
print(n) # 3
len() works the same way on dictionaries as on lists and strings:
empty = {}
print(len(empty)) # 0
d = {"a": 1, "b": 2}
print(len(d)) # 2
Assignment
Follow the Coding Tutorial and get the number of entries in scores!
Hint
Use len(scores) and assign it to n.
Free plan: 30 tutor messages. PRO is unlimited.
Saved
Ctrl + Enter to check
