How to Find the Length of a Dictionary in Python?
len()
function returns the number of key-value pairs i.e., the number of items present in the dictionary.
# Dictionary
student_scores = {
'John': 85,
'Alice': 92,
'Bob': 78
}
# Find the length of the dictionary
length = len(student_scores)
Syntax
length = len(dictionary)
How to Find Length of a Dictionary in Python
# Dictionary
student_scores = {
'John': 85,
'Alice': 92,
'Bob': 78
}
# Find the length of the dictionary
length = len(student_scores)
print(length)
# Output: 3