What is the most efficient way to alternate taking values from different iterators in Python, so that, for example, alternate(xrange(1, 7, 2), xrange(2, 8, 2)) would yield 1, 2, 3, 4, 5, 6. I know one way to implement it would be:
def alternate(*iters):
while True:
for i in iters:
try:
yield i.next()
except StopIteration:
pass
But is there a more efficient or cleaner way? (Or, better yet, an itertools function I missed?)
what about zip? you may also try izip from itertools
if this is not what you want, please give more examples in your question post.