Kodeclik Blog


Python Dictionary Length

Dictionaries are very useful data structures that can hold key-value pairs. Here is an example of a dictionary that stores months and the number of days in each.
Note that the keys are strings representing names of months. The values are integers denoting the number of days in the respective month.

Finding the length of the dictionary

Use the len() function on the dictionary to find its length. For instance:
This outputs:

Find the length of a dictionary’s keys

We can use the same approach to first extract the keys of the dictionary and then apply len() on that result. Needless to say it will give the same answer:
The result is:

Find the length of a dictionary’s values

Similarly, we can first extract the values of the dictionary and then apply len() on that result. Again we will get the same answer:
The result is:

Finding the length of an empty dictionary

If you have an empty dictionary, the length as expected will be zero.
The output is:

Finding the length of a nested dictionary

Let us assume we have a dictionary corresponding to all the meta data about a movie, like so:
The output is:
The answer is 4 because there are four top-level keys and four top-level values. Note that even though the value for the “cast” key is itself a dictionary (with four keys and four values) the len() function does not flatten this dictionary out and give the result as 7. The total length of the harrypotter dictionary is only 4.
If you would like to flatten the dictionary inside the dictionary and count the total as 3+4, we need to write a function to do so. We should probably also account for the case where there are dictionaries within dictionaries within dictionaries, and so on. Hence, it is good to write a recursive function that drills as deep into the dictionary as necessary.
Here is such a recursive function:
The function recursive_length() takes a dictionary “d” as input, tests, goes through each value of the dictionary, and keeps a running count of values encountered. If the value is itself a dictionary, it calls recursive_length() recursively on that value. Else it adds 1 to the running count and returns the answer.
If you apply it on the harrypotter dictionary like so:
we will get:
as intended.
Let us try it on multiple levels of nesting and with multiple instances of nesting in a given dictionary:
The result is:
(You can verify that this is true.) Note that this approach to counting the keys does not count the keys such as “released”, “cast”, “crew”, “Direction”, and “Writing” whose values are dictionaries themselves. If you desire to count these keys as well you need to pass on a counter every time you do the recursion (beginning at zero). See the code below:
Note that it takes two arguments, a dictionary and a sum. Everytime a recursive call is made, this sum is set to 1 (to account for the key whose value is itself a dictionary).
When we try it with:
we will get the answer we are expecting:
In this blogpost, we have learnt how to find the length of a dictionary in Python and how to recursively find the length when there are dictionaries embedded within dictionaries. Enjoy experimenting with the functions introduced here! Learn more about dictionaries in our posts about empty dictionaries in Python, and how to initialize a dictionary in Python.
Interested in more things Python? See our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2021. All rights reserved.