How to get the size of a dictionary in Python?
To get the size of a dictionary in Python, use the len() function.
len() function
The size of a dictionary is the number of elements in it, or in other words it's length.
To find the length of a dictionary, use the len() function as shown below.
# find length of a dictionary using len() function
myDict = {'Id': 1, 'Name': 'Ashley', 'Age': 27}
dictLength = len(myDict)
print(dictLength)
The len() function returns the total length of the dictionary, equal to the number of elements in the dictionary.
In this example, the dictionary contains 3 keys and therefore 3 is returned.
Please note, that key/value pairs are counted as one.
3
further information
The len() function returns the number of element in a given object. It can be used to count the size of iterables like dictionaries and lists.