[Python-ideas] Rewriting the "roundrobin" recipe in the itertools documentation

Alon Snir AlonSnir at hotmail.com
Tue Nov 21 08:50:38 EST 2017


def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    nexts = [ iter(it).__next__ for it in iterables ]
    i = 0
    while nexts:
        i %= len(nexts)
        try:
            yield nexts[i]()
        except StopIteration:
            del nexts[i]
        else:
            i += 1

Regards
Alon Snir


More information about the Python-ideas mailing list