Interesting list() un-optimization

Terry Reedy tjreedy at
Thu Mar 7 21:34:33 CET 2013

On 3/7/2013 11:20 AM, Christian Heimes wrote:

> But iterators have a length hint method that are used for some
> optimizations and preallocations, too.

This is easy when the base iterable has a length method, as do range 

>>>> i = iter(range(10))
>>>> i.__length_hint__()
> 10

And the length_hint can (should be) decremented with each next call.

 >>> next(i); next(i)
 >>> i.__length_hint__()

Terry Jan Reedy

More information about the Python-list mailing list