Created
December 9, 2012 05:12
-
-
Save mcho421-snippets/4243437 to your computer and use it in GitHub Desktop.
Python: Flatten irregular list of lists
This file contains 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(l): | |
for el in l: | |
if isinstance(el, collections.Iterable) and not isinstance(el, basestring): | |
for sub in flatten(el): | |
yield sub | |
else: | |
yield el |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment