Skip to content

Instantly share code, notes, and snippets.

@tef
Created September 10, 2012 03:55
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 tef/3688790 to your computer and use it in GitHub Desktop.
Save tef/3688790 to your computer and use it in GitHub Desktop.
def flatten(source):
stack = source[::-1]
while stack:
top = stack.pop()
if hasattr(top, '__iter__'):
stack.extend(reversed(top))
else:
yield top
print list(flatten([1,[2,3,[4,5,[6],7],8,[9,[10,11],[12],[[13]],[[[14,15]]],16]]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment