Find the size of a Dictionary in Python


Working with Python dictionaries, a powerful data type, necessitates an understanding of their size and memory requirements. Assuming you're working with word references in Python, you might have to decide their size to allot adequate memory or perform explicit activities. Fortunately, the size of a dictionary can be determined with the help of Python's built-in len() function. A quick and simple way to gauge the dictionary's contents is to use this function, which returns the total number of key-value pairs in the dictionary. Nonetheless, remember that the size of a word reference can differ contingent upon the quantity of sections it contains

Syntax

The syntax to determine a dictionary's size is rather straightforward. The len() function may be used to calculate the number of entries in a dictionary.

size = len(dictionary)

Algorithm

Use the len() method to determine a dictionary's size. The dictionary's item count is returned by this function. This is how it goes −

  • Create a dictionary.

  • To determine the dictionary's size, use the len() method.

  • Print out the dictionary's size.

Example 1

my_dict = {"apple": 2, "banana": 4, "orange": 3}
size = len(my_dict)
print(size)

Output

3

Explanation

In this instance, a dictionary named my dict has three key-value pairs specified and the size variable is then updated using the information we obtained using the len() method to determine the dictionary's size. Lastly, we use the print() method to output the dictionary's size.

Example 2

my_dict = {}
my_dict["apple"] = 2
my_dict["banana"] = 4
my_dict["orange"] = 3
size = len(my_dict)
print(size)

Output

3

Explanation

Create a blank dictionary named "my dict" and add three key-value pairs to it using the notation enclosed in square brackets. To determine the dictionary's size, use the len() function and save the result in the size variable. It is just the same as the previous example.

Getting the Memory Size of the Dictionary

Using the getsizeof() function from the sys module, one may determine the size of a dictionary in Python. The object's size in bytes is returned by this function. Just send the dictionary as an input to the getsizeof() function to get the dictionary's size.

import sys
my_dict = {"apple": 1, "banana": 2, "cherry": 3}
size = sys.getsizeof(my_dict)
print("Size of dictionary:", size, "bytes")

Output

Size of dictionary: 240 bytes

Provide the three key-value pairs from your dictionary, named my dict, as a parameter to the getsizeof() function, and then assign the outcome to the size variable. After that, use the print() method to output the dictionary's size in bytes. The size is shown in bytes, as you can see.

Applications

Understanding a dictionary's size is useful for filtering and sorting operations, memory allocation, and making sure there is enough room to store all of its components. The built-in Python function "len()" may be used to count the number of key-value pairs in a dictionary, making it simple to gauge the dictionary's size. The size of a dictionary can vary based on the sorts of data it stores and the qualities of the keys, which is crucial to bear in mind since it might affect how hard the work at hand is as a whole.

Conclusion

As it may be used to allocate memory or make sure that the dictionary has adequate space to retain all of its information, dictionary size estimation is a crucial component of programming. The "len()" function in Python may be used to quickly and conveniently find out how big a dictionary is, as well as to use dictionaries and run different operations on them. This approach is flexible and may be applied in a number of situations, such as comparing the sizes of two dictionaries to ascertain which is empty. You may manage dictionaries with confidence and easily by making use of the capabilities of such calculations.

Updated on: 09-May-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements