[Python-ideas] Length hinting and preallocation for container types

Terry Reedy tjreedy at udel.edu
Wed Mar 6 05:06:23 CET 2013

On 3/5/2013 2:50 PM, Ethan Furman wrote:

> I suspect the new behavior would be most useful when you don't know
> precisely how large the final list will be: overallocate (possibly by a
> large margin), then __exit__ returns the unused portion back to the pool).

If one counts the items as they are added, it is easy to delete extras.

ll = [None]*200
for i in range(195):
   ll[i] = i
del ll[195:]
# [190, 191, 192, 193, 194]

del ll[ll.index(None):]
would do the same thing and be faster than counting one by one.

Terry Jan Reedy

More information about the Python-ideas mailing list