[Python-ideas] Rewriting the "roundrobin" recipe in the itertools documentation
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
next(roundrobin(*([]*1000 + []])))
The following implementation has linear time.
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
nexts = [iter(it).__next__ for it in iterables]
next_nexts = 
append = next_nexts.append
for next in nexts:
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