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

1 Answer

+2 votes
by (281k 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)
>>> len(aa.keys())
>>> len(aa.values())