Converting a flat list to a list of tuples

Peter Otten __peter__ at web.de
Wed Nov 23 07:51:31 EST 2005


Fredrik Lundh wrote:

> Bengt Richter wrote:
> 
>> Are you thinking of something like lines from a file, where there might
>> be chunky buffering? ISTM that wouldn't matter if the same next method
>> was called. Here we have multiple references to the same iterator. Isn't
>> e.g. buiding a plain tuple defined with evaluation one element at a time
>> left to right?
> 
> yeah, but what says that the iterator has to be called during tuple
> construction?
> 
>     while 1:
>         for each sequence:
>             # optimize cache behaviour!
>             grab up to N items from each iterator
>         M = length of shortest output list
>         for i in range(M):
>             build tuple and append
>         if M != N:
>             break

Wouldn't every attempt to introduce such an optimization be shot down by the
likes of

def exponential():
    for i in xrange(sys.maxint):
        time.sleep(2**i)
        yield "whatever"

def const():
    for i in xrange(5): yield i

zip(exponential(), const())

To say it another way, aren't the problems that can be created by not
specifying zip() behaviour in a way that allows the zip(it, it) trick worse
than those you want to prevent?

Peter




More information about the Python-list mailing list