Python: Get Size of Dictionary

python_tutorials

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",

 

 

To finish reading, please visit source site