Skip to content

Instantly share code, notes, and snippets.

@cameronp98
Last active January 2, 2016 18:19
Show Gist options
  • Save cameronp98/8342960 to your computer and use it in GitHub Desktop.
Save cameronp98/8342960 to your computer and use it in GitHub Desktop.
Generator to flatten nested lists in Python. (Only works in 3.3+ w/ 'yield from' syntax)
def flatten(seq):
for item in seq:
if hasattr(item, "__iter__"):
yield from flatten(item)
else:
yield item
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment