• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python getsizeof() Function – Get Size of Object

Python getsizeof() Function – Get Size of Object

June 5, 2022 Leave a Comment

The Python getsizeof() function allows you to get the size (in bytes) of objects in your Python programs.

import sys

print(sys.getsizeof(3))
print(sys.getsizeof("hello"))
print(sys.getsizeof([0, 1, 2, 3]))

#Output:
28
54
120

When working with objects in Python, the ability to easily be able to get different pieces of information about the objects can be valuable.

One such piece of information which is valuable is the size of an object in memory.

When working with big datasets or collections of data with many elements, sometimes we need to be conscious of how much memory we are using.

The Python sys module getsizeof() function returns the size of a given object in bytes. The object can be any type of object.

Below are three examples showing you how to use getsizeof() in your Python code.

import sys

print(sys.getsizeof(3))
print(sys.getsizeof("hello"))
print(sys.getsizeof([0, 1, 2, 3]))

#Output:
28
54
120

Hopefully this article has been useful for you to learn how to use the Python getsizeof() function.

Other Articles You'll Also Like:

  • 1.  Combine Sets in Python
  • 2.  Using Python to Remove First Character from String
  • 3.  Using Python to Return Two Values from Function
  • 4.  Using Python to Find Second Smallest Value in List
  • 5.  Using Python to Count Even Numbers in List
  • 6.  Read First Line of File Using Python
  • 7.  pandas idxmin – Find Index of Minimum Value of Series or DataFrame
  • 8.  Using Python to Flatten Array of Arrays
  • 9.  How to Check if Character is Uppercase in Python
  • 10.  pandas ceil – Find the Ceiling of a Column Using Numpy ceil

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy