Skip to content

Instantly share code, notes, and snippets.

@marcodebe
Last active April 23, 2019 20:39
Show Gist options
  • Save marcodebe/e04e4f24dd82739289971996d33991c3 to your computer and use it in GitHub Desktop.
Save marcodebe/e04e4f24dd82739289971996d33991c3 to your computer and use it in GitHub Desktop.
Flatten list in Python
def flatten(l):
if isinstance(l, list):
out = []
out = flatten(l.pop()) + out
if l:
out = flatten(l) + out
return out
return [l]
# Example
l = [3, 1, [4, 1], [[5], [9]], [2, [6, 5]]]
print(flatten(l))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment