Python: Get Size of Dictionary


In this article, we'll take a look at how to find the size of a dictionary in Python.

Dictionary size can mean its length, or space it occupies in memory. To find the number of elements stored in a dictionary we can use the len() function.

To find the size of a dictionary in bytes we can use the getsizeof() function of the sys module.

To count the elements of a nested dictionary, we can use a recursive function.

Finding the Size of the Dictionary

The len() function is widely used to determine the size of objects in Python. In our case, passing a dictionary object to this function will return the size of the dictionary i.e. the number of key-value pairs present in the dictionary.

Because these objects keep track of their length, this operation has an O(1) time complexity:

my_dict = {1: "a", 2: "b"}
print("The length of the dictionary is {}".format(len(my_dict)))

The above snippet returns this output:

The length of the dictionary is 2

Finding the Size of the Dictionary in Bytes

The memory size of the dictionary object in bytes can be determined by the getsizeof() function. This function is available from the sys module. Like len(), it can be used to find the size of any Python object.

This is particularly useful when we need code that needs to be performant, and/or requires regular monitoring. Let's take our previous example, and get a dictionary's size in bytes instead of the number of elements:

import sys

my_dict = {1: "a", 2: "b"}
print("The size of the dictionary is {} bytes".format(sys.getsizeof(my_dict)))

The resulting output is:

The size of the dictionary is 232 bytes

Finding the Size of Nested Dictionaries

A nested dictionary is a dictionary inside a dictionary, or a dictionary with multiple levels of key-value pairs. These nested dictionaries help in simplifying complex structures like JSON responses from APIs.

These look something along the lines of:

{"dict1": {"dict2": "value 1"}}

Using the len() to get the count of all key-value pairings will not work as it gives the size of the object for the first level of keys only. To find the number of all the nested keys, we can write a custom recursive function to count the keys. This function would take a dictionary and a counter as arguments and iterate through each key.

For every iteration, the function checks if the instance of the key under consideration is a dictionary. If it's true, the function is recursively called again by appending the counter variable to counter+1 and passing the dictionary under evaluation as arguments.

This recursive function will exist upon the complete iteration, returning the length of the dictionary as the variable: counter.

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

If the key isn't a dictionary instance, the counter is simply appended to counter+1. The function returns the counter value as a result of the iteration which gives the size of the dictionary under evaluation.

Hence, the count of the nested keys is evaluated using this function as shown below:

def count_keys(dict_, counter=0):
    for each_key in dict_:
        if isinstance(dict_[each_key], dict):
            # Recursive call
            counter = count_keys(dict_[each_key], counter + 1)
            counter += 1
    return counter

my_dict = {
               'first_name': 'Sherlock',
               'Last_name': 'Holmes'
                   'Street': '221B Baker Street'
           'City': 'London',
           'Country': 'United Kingdom'

print('The length of the nested dictionary is {}'.format(count_keys(my_dict)))

And when the snippet gets executed, we get the following output corresponding to the number of keys present in the dictionary:

The length of the nested dictionary is 8


In this article, we have explored the methods to calculate the size and length of dictionaries and nested dictionaries.

These functions can be very helpful in serving JSON objects over APIs: there are limits imposed by web servers for the size of JSON objects served over APIs and these functions can be used to keep the length and size in check.

Last Updated: February 24th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Sathiya Sarathi GunasekaranAuthor

Pythonist šŸ| Linux Geek who codes on WSL | Data & Cloud Fanatic | Blogging Advocate | Author

Ā© 2013-2024 Stack Abuse. All rights reserved.