diff -r d6aa3fa646e2 Doc/library/itertools.rst --- a/Doc/library/itertools.rst Fri Feb 21 18:30:53 2014 -0500 +++ b/Doc/library/itertools.rst Fri Feb 21 22:34:11 2014 -0700 @@ -716,16 +716,9 @@ def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" - # Recipe credited to George Sakkis - 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)) + sentinel = object() + it = chain.from_iterable(zip_longest(fillvalue=sentinel, *iterables)) + return (i for i in it if i is not sentinel) def partition(pred, iterable): 'Use a predicate to partition entries into false entries and true entries' diff -r d6aa3fa646e2 Lib/test/test_itertools.py --- a/Lib/test/test_itertools.py Fri Feb 21 18:30:53 2014 -0500 +++ b/Lib/test/test_itertools.py Fri Feb 21 22:34:11 2014 -0700 @@ -1958,16 +1958,9 @@ >>> def roundrobin(*iterables): ... "roundrobin('ABC', 'D', 'EF') --> A D E B F C" -... # Recipe credited to George Sakkis -... 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)) +... sentinel = object() +... it = chain.from_iterable(zip_longest(fillvalue=sentinel, *iterables)) +... return (i for i in it if i is not sentinel) >>> def powerset(iterable): ... "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"