Skip to content

Instantly share code, notes, and snippets.

@rbarzic
Created October 23, 2016 13:10
Show Gist options
  • Save rbarzic/91025d89da3858bd1e5d27dd659529f5 to your computer and use it in GitHub Desktop.
Save rbarzic/91025d89da3858bd1e5d27dd659529f5 to your computer and use it in GitHub Desktop.
# 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