[docs] [issue20727] Improved roundrobin itertools recipe

David Lindquist report at bugs.python.org
Sat Feb 22 07:36:58 CET 2014

New submission from David Lindquist:

The roundrobin example in the Recipes section of the itertools documentation (http://docs.python.org/3/library/itertools.html#itertools-recipes) is overly complex. Here is a more straightforward implementation:

def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    sentinel = object()
    it = chain.from_iterable(zip_longest(fillvalue=sentinel, *iterables))
    return (i for i in it if i is not sentinel)

Not only is it one-third the lines of the existing example, benchmarks show it to be more than twice as fast.

See attached patch file.

assignee: docs at python
components: Documentation
files: roundrobin.patch
keywords: patch
messages: 211907
nosy: david.lindquist, docs at python
priority: normal
severity: normal
status: open
title: Improved roundrobin itertools recipe
versions: Python 2.7, Python 3.1, Python 3.2, Python 3.3, Python 3.4, Python 3.5
Added file: http://bugs.python.org/file34180/roundrobin.patch

Python tracker <report at bugs.python.org>

More information about the docs mailing list