[Python-ideas] Length hinting and preallocation for container types
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
# [190, 191, 192, 193, 194]
would do the same thing and be faster than counting one by one.
Terry Jan Reedy
More information about the Python-ideas