There are two problems with your attempt:
- You don't wrap each object in
iters with iter() so it will fail with iterables such as list; and
- By
passing on StopIteration your generator is an infinite loop.
Some simple code that does solves both those issues and is still easy to read and understand:
def alternate(*iters):
iters = [iter(i) for i in iters]
while True:
for i in iters:
yield next(i)
>>> list(alternate(range(1, 7, 2), range(2, 8, 2)))
[1, 2, 3, 4, 5, 6]