summaryrefslogtreecommitdiff
path: root/itertoolset.py
blob: 13bb896b09fc556f5d172e3b2c49e496a426e8c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
from itertools import cycle, islice

def roundrobin(*iterables):
    """roundrobin('ABC', 'D', 'EF') --> A D E B F C"""
    pending = len(iterables)
    nexts = cycle(iter(it).next for it in iterables)
    while pending:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            pending -= 1
            nexts = cycle(islice(nexts, pending))