Apparently list(a) doesn't overallocate, [x for x in a] overallocates at some points, and [*a] overallocates all the time?
Here are sizes n from 0 to 12 and the resulting sizes in bytes for the three methods:
0 56 56 56
1 64 88 88
2 72 88 96
3 80 88 104
4 88 88 112
5 96 120 120
6 104 120 128
7 112 120 136
8 120 120 152
9 128 184 184
10 136 184 192
11 144 184 200
12 152 184 208
Computed like this, reproducable at repl.it, using Python 3.8:
from sys import getsizeof
for n in range(13):
a = [None] * n
print(n, getsizeof(list(a)),
getsizeof([x for x in a]),
getsizeof([*a]))
So: How does this work? How does [*a] overallocate? Actually, what mechanism does it use to create the result list from the given input? Does it use an iterator over a and use something like list.append? Where is the source code?
(Colab with data and code that produced the images.)
Zooming in to smaller n:
Zooming out to larger n:



[*a]appears to behave as usingextendon an empty list. - jdehesalist(a)operates entirely in C; it can allocate the internal buffer node by node as it iterates overa.[x for x in a]just usesLIST_APPENDa lot, so it follows the normal "overallocate a little, reallocate when necessary" pattern of a normal list.[*a]usesBUILD_LIST_UNPACK, which... I don't know what that does, other than apparently over-allocate all the time :) - chepnerlist(a)and[*a]are identical, and both overallocate compared to[x for x in a], so...sys.getsizeofmight not be the right tool to use here. - chepnersys.getsizeofis the right tool, it just shows thatlist(a)used to overallocate. Actually What’s New In Python 3.8 mentions it: "The list constructor does not overallocate [...]". - Stefan Pochmann