Skip to content

Instantly share code, notes, and snippets.

@austinbrian
Last active February 16, 2021 14:16
Show Gist options
  • Save austinbrian/2f69160f32fe31db99cd23f62f9670e1 to your computer and use it in GitHub Desktop.
Save austinbrian/2f69160f32fe31db99cd23f62f9670e1 to your computer and use it in GitHub Desktop.
Flatter list out of a nested list of lists
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