Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python None list size

I was just checking the size of some datatypes in Python 3 and I observed this.

import sys

val = None
print(sys.getsizeof(val))

The output was 16 as expected.

I tried making a list of 1000 locations of None and I expected the size to be 16*1000 = 16000 or more. But the result I got was different.

import sys

val = [None]*1000

print(sys.getsizeof(val))

The output was 8064. Nearly the half of the size I expected.

What is the reason for this.? Why memory allocated is less.?

like image 485
Sreeram TP Avatar asked Sep 29 '26 13:09

Sreeram TP


1 Answers

import sys

val = None

print(sys.getsizeof(val))

Answer:

16

val = []

print(sys.getsizeof(val))

Answer: 

72

val = [None]

print(sys.getsizeof(val))

Answer:

80

so [None]*1000 = 1000* 8 + 72 = 8072

Note: No of bytes may vary depending on the environment

like image 163
kartheek Avatar answered Oct 01 '26 02:10

kartheek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!