[Python-ideas] Rewriting the "roundrobin" recipe in the itertools documentation
Serhiy Storchaka
storchaka at gmail.com
Tue Nov 21 04:44:33 EST 2017
The roundrobin() implementation in recipes has quadratic time for large
number of iterables. As well as all other proposed implementations. This
is a problem if you use it with hundreds or thousands of iterables. For
example:
list(roundrobin(*([[1]]*1000)))
next(roundrobin(*([[]]*1000 + [[1]]])))
The following implementation has linear time.
def roundrobin(*iterables):
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
nexts = [iter(it).__next__ for it in iterables]
while nexts:
next_nexts = []
append = next_nexts.append
for next in nexts:
try:
yield next()
except StopIteration:
pass
else:
append(next)
nexts = next_nexts
Actually it expands cycle() in Python. And this makes it slower for
smaller number of iterations.
More information about the Python-ideas
mailing list