Given two lists, I want to merge them so that all elements from the first list are even-indexed (preserving their order) and all elements from second list are odd-indexed (a
This is simple enough although not nearly as flexible as roundrobin
:
def paired(it1, it2):
it2 = iter(it2)
for item in it1:
yield item
yield next(it2)
tested in 2.7.5:
>>> x = [0, 1, 2]
>>> y = [3, 4]
>>> print list(paired(x, y))
[0, 3, 1, 4, 2]
Note that it stops as soon as the list y
runs out (because next(it2)
raises StopIteration).