How to know the Size of an Object in Python


If you want to know the size of objects in Python you can make use of the getsizeof() function from the sys module.

Syntax:
sys.getsizeof(object[, default])

Return the size of an object in bytes.

Read more: https://docs.python.org/3/library/sys.html#sys.getsizeof

Examples:
>>> import sys
>>> 
>>> data_list = [10,20,30,40,60,80]
>>> 
>>> sys.getsizeof(data_list)
104
>>> 
>>> city = "New York"
>>> 
>>> sys.getsizeof(city)
57
>>> 
>>> no = 1
>>> 
>>> sys.getsizeof(no)
28
Python Get Size of Objects Examples
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap