Created
October 23, 2016 13:10
-
-
Save rbarzic/91025d89da3858bd1e5d27dd659529f5 to your computer and use it in GitHub Desktop.
Flatten list in Python - Full credits to http://rightfootin.blogspot.fr/2006/09/more-on-python-flatten.html
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
# http://rightfootin.blogspot.fr/2006/09/more-on-python-flatten.html | |
def flatten(l, ltypes=(list, tuple)): | |
ltype = type(l) | |
l = list(l) | |
i = 0 | |
while i < len(l): | |
while isinstance(l[i], ltypes): | |
if not l[i]: | |
l.pop(i) | |
i -= 1 | |
break | |
else: | |
l[i:i + 1] = l[i] | |
i += 1 | |
return ltype(l) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment