• 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
  • VBA
  • 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.  Format Numbers as Currency with Python
  • 2.  Skip Numbers in Python Range
  • 3.  How to Find the Longest String in List in Python
  • 4.  Using Python to Remove Apostrophe From String
  • 5.  PROC MIXED Equivalent in Python for Least Squared Means ANOVA
  • 6.  Multiple Condition if Statements in Python
  • 7.  List of Turtle Shapes in Python
  • 8.  Python Split Tuple into Multiple Variables
  • 9.  Using Python to Convert Integer to String with Leading Zeros
  • 10.  How to Rotate String in Python

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy