[*a]
is internally doing the C equivalent of:
- Make a new, empty
list
- Call
newlist.extend(a)
- Returns
list
.
So if you expand your test to:
from sys import getsizeof
for n in range(13):
a = [None] * n
l = []
l.extend(a)
print(n, getsizeof(list(a)),
getsizeof([x for x in a]),
getsizeof([*a]),
getsizeof(l))
Try it online!
you'll see the results for getsizeof([*a])
and l = []; l.extend(a); getsizeof(l)
are the same.
This is usually the right thing to do; when extend
ing you're usually expecting to add more later, and similarly for generalized unpacking, it's assumed that multiple things will be added one after the other. [*a]
is not the normal case; Python assumes there are multiple items or iterables being added to the list
([*a, b, c, *d]
), so overallocation saves work in the common case.
By contrast, a list
constructed from a single, presized iterable (with list()
) may not grow or shrink during use, and overallocating is premature until proven otherwise; Python recently fixed a bug that made the constructor overallocate even for inputs with known size.
As for list
comprehensions, they're effectively equivalent to repeated append
s, so you're seeing the final result of the normal overallocation growth pattern when adding an element at a time.
To be clear, none of this is a language guarantee. It's just how CPython implements it. The Python language spec is generally unconcerned with specific growth patterns in list
(aside from guaranteeing amortized O(1)
append
s and pop
s from the end). As noted in the comments, the specific implementation changes again in 3.9; while it won't affect [*a]
, it could affect other cases where what used to be "build a temporary tuple
of individual items and then extend
with the tuple
" now becomes multiple applications of LIST_APPEND
, which can change when the overallocation occurs and what numbers go into the calculation.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…