Last active
January 2, 2016 18:19
-
-
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)
This file contains hidden or 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
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