Start learning free

LESSON 126 OF 673 · DICTIONARIES

Dictionary Length in Python

Lesson

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.

STEP 1 OF 1
    Ctrl + Enter to check