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.

_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to