Skip to content

Instantly share code, notes, and snippets.

@mitchellrj
Created October 12, 2011 14:44
Show Gist options
  • Save mitchellrj/1281400 to your computer and use it in GitHub Desktop.
Save mitchellrj/1281400 to your computer and use it in GitHub Desktop.
Flatten in Python
def flatten(iterable):
if not isinstance(iterable, (list, tuple)):
yield iterable
else:
for i in iterable:
if isinstance(i, (list, tuple)):
for j in flatten(i):
yield j
else:
yield i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment