Skip to content

Instantly share code, notes, and snippets.

@escuccim
Created April 17, 2018 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save escuccim/bcde1e1220374c7d32517889ee7a6e99 to your computer and use it in GitHub Desktop.
Save escuccim/bcde1e1220374c7d32517889ee7a6e99 to your computer and use it in GitHub Desktop.
Python Function to Flatten Nested Lists
# this works just like "unlist" in R
def flatten(l):
out = []
for item in l:
if isinstance(item, (list, tuple)):
out.extend(flatten(item))
else:
out.append(item)
return out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment