How many different ways are there to find the length of a dictionary in Python?

+2 votes

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

>>>