[issue32099] Use range in itertools roundrobin recipe

Serhiy Storchaka report at bugs.python.org
Tue Nov 21 12:28:56 EST 2017


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

Today I have published a similar recipe on Python-Ideas. It uses popleft/append instead of __getitem__/rotate.

def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    nexts = deque(iter(it).__next__ for it in iterables)
    popleft = nexts.popleft
    append = nexts.append
    while nexts:
        next = popleft()
        try:
            yield next()
        except StopIteration:
            pass
        else:
            append(next)

It is faster (10-25%) in all microbenchmarks that I did (Steven's benchmarks for small number of iterables and my examples for large number of iterables).

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32099>
_______________________________________


More information about the Python-bugs-list mailing list