Laser
Laser

Reputation: 6960

why sys.getsizeof returns the same output for very big lists

python version: Python 3.6.0b3 (default, Nov 16 2016, 16:55:09)

I have the following output:

>>> sys.getsizeof([0 for x in range(10**7)])
81528056
>>> sys.getsizeof([0 for x in range(10**8)])
859724472
>>> sys.getsizeof([0 for x in range(10**8+100500)])
859724472
>>> sys.getsizeof([0 for x in range(10**9)])
8058558880

Length of lists:
>>> len([0 for x in range(10**8)])
100000000
>>> len([0 for x in range(10**8+100500)])
100100500
>>>

Why did I have the same getsizeofoutput for lists which len are: 10**8 and 10**8+100500 ?

Upvotes: 2

Views: 164

Answers (2)

Kevin Kendzia
Kevin Kendzia

Reputation: 248

sys.getsizeof shows the size in byte. Your lists with [0 for x in range(10**8...)]) can both be displayed with 859724472 bytes.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798576

When a list is resized larger its length is increased by a bit more than 1/8 of the requested length. 100500 is far less than 1/8 of 10**8 which means that the list likely doesn't need to be resized from its previous resize in order to accommodate the new entries.

Upvotes: 3

Related Questions