itertools.izip brokeness

Michael Spencer mahs at telcopartners.com
Fri Jan 6 00:51:59 EST 2006


Paul Rubin wrote:
> Michael Spencer <mahs at telcopartners.com> writes:
>>      for i in range(10):
>>          result = []
>>          ...
> 
> Do you mean "while True: ..."?
> 
oops, yes!

so, this should have been:

from itertools import repeat

def izip2(*iterables, **kw):
     """kw:fill. An element that will pad the shorter iterable"""
     fill = repeat(kw.get("fill"))
     iterables = map(iter, iterables)
     iters = range(len(iterables))

     while True:
         result = []
         for idx in iters:
             try:
                 result.append(iterables[idx].next())
             except StopIteration:
                 iterables[idx] = fill
                 if iterables.count(fill) == len(iterables):
                     raise
                 result.append(fill.next())
         yield tuple(result)

Michael




More information about the Python-list mailing list