When the new element is added to the list, the size of the list needs to be increased. Python slightly overallocates is some cases to avoid resizing for each element.
Yes. Applying getsizeof to the empty list returns 56 (not 72), and the lists to which I applied getsizeof in both cases were not empty (consisting of one element, β0β), so it looks like overallocation occurs in both cases, but why it adds different numbers of extra cells?
Here is an additional experiment proving that overallocations occurred in both cases:
import sys
a = []
for _ in range(5):
a += [0]
print(sys.getsizeof(a), end = ' ')
this returns: β72 72 120 120 120β, which means that in the first case, after execution of βa += [0]β for the first time, we got 2 extra cells.
After replacing βa += [0]β with βa.append(0)β in this fragment, we get β88 88 88 88 120β, which means that in the second case, after execution of βa.append(0)β for the first time, we got 4 extra cells.
So we get overallocations in both cases, but we get different numbers of extra cells.
Well, apparently itβs assumed that append will be done often, so you really want overallocation.
And apparently itβs assumed that += will be the only one or that the next extension will be large enough that it needs resizing anyway, so it only calls list_preallocate_exact. Which only overallocates by at most a single element, and only for alignment reasons. See the explanation in its source code.
Sorry, I think my previous message was a little bit obscure. Let me clarify. In the previous messages of the current thread, we established that there is some difference between a += x and a.append(x) (a is a list).
Now, I think that if there is certain difference in behaviors of these methods, then this difference should appear when these methods are called from within lists of different initial sizes. But in fact, this difference appears only when these methods are called from within empty lists! Why?
The program below prints out all initial lengths of lists when a += x and a.append(x) return different results. And it turns out that there is just one such a list β an empty one! So if there is difference between this methods, why this difference is so tiny?
import sys
size_of_header = sys.getsizeof([])
size_of_item = sys.getsizeof([0]) - size_of_header
for i in range(10**4):
a = [0]*i
a += [0]
sa = sys.getsizeof(a)
b = [0]*i
b.append(0)
sb = sys.getsizeof(b)
if sa != sb:
print(i)