Created
April 17, 2018 09:01
-
-
Save escuccim/bcde1e1220374c7d32517889ee7a6e99 to your computer and use it in GitHub Desktop.
Python Function to Flatten Nested Lists
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
# 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