Created
September 7, 2013 19:39
-
-
Save zabbarob/6478583 to your computer and use it in GitHub Desktop.
flatten nested lists in python
From http://stackoverflow.com/questions/2158395/flatten-an-irregular-list-of-lists-in-python
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(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