Memory size of a dictionary
Write a Python program to get the memory size of a dictionary with sys.getsizeof, and its number of items.
{"name": "virat", "sport": "cricket"}2 items True | bytes on this Python: 184
Approach
sys.getsizeofgives the size of the dict object itself,lenthe item count.
Solution
import sys d = {"name": "virat", "sport": "cricket"}print(len(d), "items")print(sys.getsizeof(d) > 0, "| bytes on this Python:", sys.getsizeof(d))2 items True | bytes on this Python: 184
The byte count depends on the Python version and platform, and it does not include the keys and values themselves.
Learn the concept
New to this topic? Read the lesson, then come back.