+1 vote
in Programming Languages by (51.1k points)
How many different ways are there to find the length of a dictionary in Python?

1 Answer

+2 votes
by (157k points)
selected by
 
Best answer

You need to use the len() function to find the length of a dictionary, i.e., how many key:value pairs are there.

You can apply the len() function to the dictionary itself or its keys or values.

Here is an example:

>>> aa={'a':1,'b':2,'c':4,'d':7}
>>> aa
{'a': 1, 'b': 2, 'c': 4, 'd': 7}
>>> len(aa)
4
>>> len(aa.keys())
4
>>> len(aa.values())
4
>>>


...