Objects | How Do I Determine The Size Of An Object In Python?

In this tutorial, we will learn How To determine the size of an object in python which we can simply find using importing sys inbuild module and using the get size of () method to get the size of the object used in python.

ObjectsThis could be used for getting the size of a single object but we can also get the size of the whole list of objects in just one to go where we just need to pass the list of objects in a parameter to get the size of all the objects present there.

Objects

As python is an objective oriented programming that deals with on=bjects and classes so there might be situations when we need multiple objects in a single program and deal with the size of the program there might be conditions when we are required to find the size of the object and to find the size of the object we have a way that we are going to discuss here.

Find The Size Of All The Objects

import sys

my_list = [1, 2, "hello", {"key": "value"}, [1, 2, 3]]
total_size = sys.getsizeof( my_list)

for item in my_list:
    total_size += sys.getsizeof( item)

print( f"The total size of the list is {total _size} bytes.")

It will give 271 bytes as result and it will print “The total size of the list is 271 bytes.” as a result after finding the total size of objects.

We can also get the size of single objects to using the same method by simply providing the object in argument in the inbuild function get size of().

 

To learn more about finding the size of objects in python visit: Size of Objects in Python.

To learn more about python programming tutorials and solve the problems related to them along with getting the concepts cleared visit: Python’s list: What is the difference Between methods Append and Extend?

And to learn more about different programming language tutorials and concepts visit beta python programming languages Solutions.

Leave a Comment

%d bloggers like this: