Thursday, February 4, 2021

Stack Abuse: Python: Get Size of Dictionary

Introduction

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 iterates 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 exits upon the complete iteration, returning the length of the dictionary as the variable: counter.

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)
        else:
            counter += 1
    return counter

my_dict = {
       'Name':
           {
               'first_name': 'Sherlock',
               'Last_name': 'Holmes'
           },
       'Locality':
           {
           'Address':
               {
                   '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

Conclusion

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 webservers for the size of JSON objects served over APIs and these functions can be used to keep the length and size in check.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...