itertools.izip brokeness

Paul Rubin http
Thu Jan 5 22:53:22 EST 2006


Michael Spencer <mahs at telcopartners.com> writes:
>      for i in range(10):
>          result = []
>          ...

Do you mean "while True: ..."?


> def izip2(*iterables, **kw):
>      """kw:fill. An element that will pad the shorter iterable"""
>      fill = repeat(kw.get("fill"))

Yet another attempt (untested, uses Python 2.5 conditional expression):

from itertools import chain, repeat, dropwhile
def izip2(*iterables, **kw):
  fill = kw.get('fill'))
  sentinel = object()
  iterables = [chain(i, repeat(sentinel)) for i in iterables]
  while True:
    t = [i.next() for i in iterables]

    # raise StopIteration immediately if all iterators are now empty
    dropwhile(lambda v: v is sentinel, t).next()

    # map all sentinels to the fill value and yield resulting tuple
    yield tuple([(v if v is not sentinel else fill) for v in t])



More information about the Python-list mailing list