CodeBlessU
learn code with examples

Size of Python Data Types

sys.getsizeof()

Use sys.getsizeof() method to find size of any variable, i.e. Number of bytes required by python to store it in memory.

Argument is mandatory.

Syntax:
sys.getsizeof( variable_name )
Return Value:
size in bytes

TypeError: Required argument 'object' (pos 1) not found

Why this error:

Are you trying to call sys.getsizeof() without arguments? OR without object in first position?

Solution:

Specify variable as first argument for sys.getsizeof().

Size of Boolean

Python boolean variable requires minimum 24 bytes on 32-bit / 64-bit system. It may vary as per hardware.

# Example, Find size of boolean

import sys

sys.getsizeof( bool() ) # prints 24
sys.getsizeof(True) # prints 28
sys.getsizeof(False) # prints 24

Size of Int

Python int variable requires minimum 24 bytes on 32-bit / 64-bit system. It may vary as per hardware.

# Find size of int

import sys

sys.getsizeof( int() ) # prints 24
sys.getsizeof(0) # prints 24
sys.getsizeof(1) # prints 28
sys.getsizeof(-2) # prints 28
sys.getsizeof(2* 10**307) # prints 164

Size of Float

Python float variable requires 24 bytes on 32-bit / 64-bit system. It may vary as per hardware.

# Find size of float

import sys

sys.getsizeof( float() ) # prints 24
sys.getsizeof(0.0) # prints 24
sys.getsizeof(1e2) # prints 24
sys.getsizeof(1.123451234512345123451234512345e2) # prints 24
sys.getsizeof(1.123451234512345123451234512345e307) # prints 24
sys.getsizeof(1.123451234512345123451234512345e-307) # prints 24
sys.getsizeof(-100.25) # prints 24

Memory requirement for List

Python list requires minimum 64 bytes on 32-bit / 64-bit system. It may vary as per hardware.

# Find size of list

import sys

sys.getsizeof( list() ) # prints 64

a = []
sys.getsizeof( a ) # prints 64

a = [1]
sys.getsizeof( a ) # prints 72
a = [1,1]
sys.getsizeof( a ) # prints 80

a = [1]*10 # assigns 10 elements i.e. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
sys.getsizeof( a ) # prints 144

a = ["Hello, let this be long string occupying more and more and more bytes", 1.2]
sys.getsizeof( a ) # prints 80
sys.getsizeof( a[0] ) # Check, individual element is occupying more bytes than overall list.
sys.getsizeof( a[1] ) # prints 24 for float

Memory requirement for Tuple

Python tuple requires minimum 48 bytes on 32-bit / 64-bit system. It may vary as per hardware. Note: Tuple occupies less memory space compare to list.

# Find size of tuple

import sys

sys.getsizeof( tuple() ) # prints 48

a = ()
sys.getsizeof( a ) # prints 48

a = (1,)
sys.getsizeof( a ) # prints 56
a = (1,1)
sys.getsizeof( a ) # prints 64

a = (1,)*10 # assigns 10 elements i.e. (1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
sys.getsizeof( a ) # prints 128

a = ("Hello, let this be long string occupying more and more and more bytes", 1.2)
sys.getsizeof( a ) # prints 64
sys.getsizeof( a[0] ) # Check, individual element is occupying more bytes than overall tuple.
sys.getsizeof( a[1] ) # prints 24 for float

Memory requirement for Array

Python array requires minimum 64 bytes on 32-bit / 64-bit system. It may vary as per hardware.

# Find size of array

import sys
import array

sys.getsizeof( array.array('b') ) # signed char, prints 64
sys.getsizeof( array.array('h') ) # signed short, prints 64
sys.getsizeof( array.array('i') ) # signed int, prints 64
sys.getsizeof( array.array('l') ) # signed long, prints 64
sys.getsizeof( array.array('q') ) # signed long long, prints 64
sys.getsizeof( array.array('f') ) # signed float, prints 64
sys.getsizeof( array.array('B') ) # unsigned char, prints 64
sys.getsizeof( array.array('H') ) # unsigned short, prints 64
sys.getsizeof( array.array('I') ) # unsigned int, prints 64
sys.getsizeof( array.array('L') ) # unsigned long, prints 64
sys.getsizeof( array.array('Q') ) # unsigned long long, prints 64
sys.getsizeof( array.array('d') ) # signed double, prints 64


sys.getsizeof( array.array('b', [-5,-4,-3,-2,-1,0,1,2,3,4] ) ) # signed char, prints 74
sys.getsizeof( array.array('h', [-5,-4,-3,-2,-1,0,1,2,3,4] ) ) # signed short, prints 84
sys.getsizeof( array.array('i', [-5,-4,-3,-2,-1,0,1,2,3,4] ) ) # signed int, prints 104
sys.getsizeof( array.array('l', [-5,-4,-3,-2,-1,0,1,2,3,4] ) ) # signed long, prints 104
sys.getsizeof( array.array('q', [-5,-4,-3,-2,-1,0,1,2,3,4] ) ) # signed long long, prints 144
sys.getsizeof( array.array('f', [-5,-4,-3,-2,-1,0,1,2,3,4] ) ) # signed float, prints 104
sys.getsizeof( array.array('B', [ 5, 4, 3, 2, 1,0,1,2,3,4] ) ) # unsigned char, prints 74
sys.getsizeof( array.array('H', [ 5, 4, 3, 2, 1,0,1,2,3,4] ) ) # unsigned short, prints 84
sys.getsizeof( array.array('I', [ 5, 4, 3, 2, 1,0,1,2,3,4] ) ) # unsigned int, prints 104
sys.getsizeof( array.array('L', [ 5, 4, 3, 2, 1,0,1,2,3,4] ) ) # unsigned long, prints 104
sys.getsizeof( array.array('Q', [ 5, 4, 3, 2, 1,0,1,2,3,4] ) ) # unsigned long long, prints 144
sys.getsizeof( array.array('d', [-5,-4,-3,-2,-1,0,1,2,3,4] ) ) # signed double, prints 144

Memory requirement for Set

Python set requires minimum 224 bytes on 32-bit / 64-bit system. It may vary as per hardware.

# Find size of set

import sys

sys.getsizeof( set() ) # prints 224

a = {1}
sys.getsizeof( a ) # prints 224
a = {1,2} 
sys.getsizeof( a ) # prints 224

a = {"Some random string with abcd", "Some random string with efgh"} 
sys.getsizeof( a ) # prints 224

a = {"abcd", "efgh",} 
sys.getsizeof( a ) # prints 224

a = set( range(1,10) )
sys.getsizeof( a ) # prints 736

Memory requirement for Dictionary

Python dictionary requires minimum 240 bytes on 32-bit / 64-bit system. It may vary as per hardware.

# Find size of dictionary

import sys

sys.getsizeof( dict() ) # prints 240

a = {1: -1}
sys.getsizeof( a ) # prints 240
a = {1: -1, 2:-2} 
sys.getsizeof( a ) # prints 240

a = {"Some random string with abcd" : -1, "Some random string with efgh" : -2} 
sys.getsizeof( a ) # prints 240

a = {"abcd" : -1, "efgh" : -2} 
sys.getsizeof( a ) # prints 240

a = dict( zip( range(1,10), range(-1,-10, -1) ) )
sys.getsizeof( a ) # prints 368

Approx size for 1M int elements in list, tuple, set, dict

Tuple requires less space compare to list. Set and dictionary requires more space compare to list and tuple.

Tuple requires average 8 bytes per element, list requires average 9 bytes per element, set requires average 33 bytes per element, dictionary requires average 41 bytes per pair. This comparison is for int data used in list, tuple, set, dict.

Memory size may not be the only criteria to select data type. Rather, time required to perform operation on data type can be critical criteria.

# Compare size for 1M int elements stored in list, tuple, set, dict

import sys

a1 = list( range(0, 10**6) )
a2 = tuple( range(0, 10**6) )
a3 = set( range(0, 10**6) )
a4 = dict( zip(range(0, 10**6),  range(0, -1 * 10**6, -1) ) )

len(a1)
len(a2)
len(a3)
len(a4)

sys.getsizeof( a1 ) / len(a1)
sys.getsizeof( a2 ) / len(a2)
sys.getsizeof( a3 ) / len(a3)
sys.getsizeof( a4 ) / len(a4)

In this page...

Copyright © All rights reserved.