Skip to content

Instantly share code, notes, and snippets.

@blakev
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blakev/3609eb6d5e2364ed83d0 to your computer and use it in GitHub Desktop.
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.
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