|
|
|
|
|
by mixmastamyk
4860 days ago
|
|
Question: def squares(n):
sq = []
for i in xrange(n):
sq.append(i*i)
return sq
A basically idiomatic version of the same in Python. No list
pre-allocation, so every iteration through the list we have the
potential to need to resize the list and copy all the data. That's
inefficient.
Is that true? I'd expect .append() to change a pointer or two, not "resize and copy" the list. Even an .insert() should just move pointers at the C-level... no need to "defrag" it. I guess the key word is potential. |
|