Here's toolz.itertoolz.interleave():<div><br></div><div><div>def interleave(seqs):</div><div> """ Interleave a sequence of sequences</div><div> >>> list(interleave([[1, 2], [3, 4]]))</div><div> [1, 3, 2, 4]</div><div> >>> ''.join(interleave(('ABC', 'XY')))</div><div> 'AXBYC'</div><div> Both the individual sequences and the sequence of sequences may be infinite</div><div> Returns a lazy iterator</div><div> """</div><div> iters = itertools.cycle(map(iter, seqs))</div><div> while True:</div><div> try:</div><div> for itr in iters:</div><div> yield next(itr)</div><div> return</div><div> except StopIteration:</div><div> predicate = partial(operator.is_not, itr)</div><div> iters = itertools.cycle(itertools.takewhile(predicate, iters))</div><div><br></div><div>At first glance, map should be e.g. six.moves.map for pythonic backward compatibility.</div><div><br></div><div>Is this slower than the linear time implementations listed here?</div><br>On Tuesday, November 21, 2017, Alon Snir <<a href="mailto:AlonSnir@hotmail.com">AlonSnir@hotmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">def roundrobin(*iterables):<br>
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"<br>
nexts = [ iter(it).__next__ for it in iterables ]<br>
i = 0<br>
while nexts:<br>
i %= len(nexts)<br>
try:<br>
yield nexts[i]()<br>
except StopIteration:<br>
del nexts[i]<br>
else:<br>
i += 1<br>
<br>
Regards<br>
Alon Snir<br>
______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="javascript:;" onclick="_e(event, 'cvml', 'Python-ideas@python.org')">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br>
</blockquote></div>