from itertools import cycle
from collections import deque
def roundrobin(*iterables):
q = deque(iter(it) for it in iterables)
for itr in cycle(q):
try:
yield itr.next()
except StopIteration:
if len(q) > 0:
q.pop()
else:
break
# EXAMPLE
for letter in roundrobin('ABC', 'DE', 'FGH'):
print letter
# prints 'A', 'D', 'F', 'B', 'E', 'G', 'C', 'H'
Diff to Previous Revision
--- revision 1 2010-07-16 14:20:29
+++ revision 2 2010-07-19 13:53:41
@@ -17,4 +17,4 @@
for letter in roundrobin('ABC', 'DE', 'FGH'):
print letter
-# prints 'A', 'D', 'F', 'B', 'E', 'G', 'C'
+# prints 'A', 'D', 'F', 'B', 'E', 'G', 'C', 'H'