Last active
February 16, 2021 14:16
-
-
Save austinbrian/2f69160f32fe31db99cd23f62f9670e1 to your computer and use it in GitHub Desktop.
Flatter list out of a nested list of 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
flat_list = [item for sublist in nested_list for item in sublist] | |
# if a list is variable, like [2,3,[2,3],[2,3]], you'll need a function | |
# this creates a generator that will do what you want | |
def flatten(lis): | |
for item in lis: | |
if isinstance(item, list) and not isinstance(item, str): | |
for x in flatten(item): | |
yield x | |
else: | |
yield item |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment