[docs] [issue32099] Use range in itertools roundrobin recipe

Raymond Hettinger report at bugs.python.org
Tue Nov 21 12:04:18 EST 2017


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

While we're on the topic, I had some thought of also adding a similar recipe to https://docs.python.org/3/library/collections.html#deque-recipes .  The alternative recipe is slower is common cases but doesn't degrade when there are a huge number of iterables:  

    def roundrobin(*iterables):
        "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
        nexts = deque(iter(it).__next__ for it in iterables)
        while nexts:
            try:
                while True:
                    yield nexts[0]()
                    nexts.rotate(-1)
            except StopIteration:
                nexts.popleft()

----------

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


More information about the docs mailing list