diff options
Diffstat (limited to 'itertoolset.py')
-rw-r--r-- | itertoolset.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/itertoolset.py b/itertoolset.py new file mode 100644 index 0000000..13bb896 --- /dev/null +++ b/itertoolset.py @@ -0,0 +1,13 @@ +from itertools import cycle, islice + +def roundrobin(*iterables): + """roundrobin('ABC', 'D', 'EF') --> A D E B F C""" + pending = len(iterables) + nexts = cycle(iter(it).next for it in iterables) + while pending: + try: + for next in nexts: + yield next() + except StopIteration: + pending -= 1 + nexts = cycle(islice(nexts, pending)) |