3

I noticed the following fact in python:

>>> (1, 2, 3).__sizeof__()
48

>>> [1, 2, 3].__sizeof__()
64

I understand the difference between list and tuple but I expected their sizeof (size of object in memory) to be the same: both come with methods and both contain same values.

Moreover, the size difference depends on items lenght:

>>> for size in (10, 100, 1000, 10000):
        tuple_ = tuple(range(size))
        list_ = list(range(size))
        print list_.__sizeof__(), tuple_.__sizeof__()     
176   104
984   824
9088  8024
90088 80024
  • How can we explain this ?
  • Where can I find good doc for python internals ?
Gaut
  • 1,255
  • 2
  • 15
  • 33

2 Answers2

4

list objects are designed to grow dynamically (through append, extend or list comprehension building). It wouldn't be performant to perform realloc (and possibly memmove) each time an element is added. So there's a "growth" algorithm which tries to predict how many elements will be needed (that's just a statistical guess of course).

That's why the actually allocated memory can be greater than the number of items.

tuple objects are immutable. There's no reason why Python would pre-allocate more elements.

Some references about list growth algorithm:

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2

According to Raymond Hettinger and tzot from Are tuples more efficient than lists in Python?,

Basically, tuples can be accessed faster than lists as it's stored more efficiently as tuples are immutable.

List, in contradiction to tuples are build up from scratch.

If you would like to iterate through a constant sequence, tuples are better since they had calculated when the python code compiled to bytecode.

Community
  • 1
  • 1
omri_saadon
  • 10,193
  • 7
  • 33
  • 58
  • 1
    I liked your comment until I saw `compile time`. Did I miss something? – Or Duan Feb 08 '17 at 08:59
  • 1
    @OrDuan , The time when python source is compiled to bytecode (which bytecode might be saved as a .py[co] file). – omri_saadon Feb 08 '17 at 09:00
  • @omri_saadon pyo/pyc files are faster in the script startup, if you use variables inside your functions/`__main__` there should not be any performance boost, am I right? Just want to make sure I get everything here. – Or Duan Feb 08 '17 at 09:13