itertools.izip brokeness

Paul Rubin http
Fri Jan 6 02:52:13 EST 2006

rurpy at writes:
> def izip4(*iterables, **kw):
>      """kw:fill. An element that will pad the shorter iterable
>         kw:infinite. Number of non-terminating iterators """

That's a really kludgy API.  I'm not sure what to propose instead:
maybe some way of distinguishing which iterables are supposed to be
iterated til exhaustion (untested):

    class Discardable(object): pass

    def izip5(*iterables, fill=None):
       """Run until all non-discardable iterators are exhausted"""
       while True:
           # exhausted iterables will put empty tuples into t
           # non-exhausted iterables will put singleton tuples there
           t = [tuple(islice(i,1)) for i in iterables]

           # quit if only discardables are left
           dropwhile(lambda i,t: (not isinstance(i, Discardable)) and len(t)),
              izip(t, iterables)).next()

           yield tuple([(v[0] if len(t) else fill) for v in t])

Then you'd wrap "infinite" and other iterators you don't need exhausted
in Discardable:

    stream = izip5(a, b, Discardable(c), d, Discardable(e), fill='')

runs until a, b, and d are all exhausted.

More information about the Python-list mailing list