Another itertool function?

Magnus Lie Hetland mlh at
Sat Apr 26 19:34:20 CEST 2003

The need for the following function just cropped up in a parser I'm
writing; it seems sufficiently general to perhaps be a candidate for
itertools -- and I can't see any way of implementing it with the
current itertools functions:

def weave(*iterables):
    iterables = map(iter, iterables)
    while iterables:
        for it in iterables[:]:
            except StopIteration:

Another possible name (which is a bit more unwieldy) would be
'roundrobin'. Another one still is iweave (to keep the naming
convention, even though here there is no clash).

This function is somewhat similar to izip, but goes through the
iterables in a round-robin fashion, dropping them as they become

I used this with items that had indices (or timestamps) associated
with them, and for each iteration I added the new ones to a heap. Then
I could get the one with the lowest index (or timestamp) etc.

I expect there would have to be more use-cases to justify it, but I
just thought I'd air the idea...

Magnus Lie Hetland               "Nothing shocks me. I'm a scientist."                                   -- Indiana Jones

More information about the Python-list mailing list