Last active
August 29, 2015 14:04
-
-
Save blakev/3609eb6d5e2364ed83d0 to your computer and use it in GitHub Desktop.
Uses itertools to add an "unravel" function...unravel takes iterables and cycles through them yielding the first element until all the elements are gone, or until a limit is reached.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import itertools | |
def unravel(*iterables, limit = None): | |
yield from itertools.islice( | |
filter(None, | |
itertools.chain.from_iterable( | |
itertools.zip_longest( | |
*iterables))), limit) | |
# a = [x for x in range(10)] | |
# b = [x for x in range(5)] | |
# c = [x for x in range(0, 20, 2)] | |
# d = [x for x in range(1, 30, 2)] | |
# print(list(unravel(a, b))) | |
# print(list(unravel(a, b, limit = 3))) | |
# print(list(unravel(a, b, c, d, limit = 20))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment