Skip to content

Instantly share code, notes, and snippets.

@edobez
Created February 7, 2020 15:19
Show Gist options
  • Save edobez/93e578d5ab81feb5dccdcee8194145ae to your computer and use it in GitHub Desktop.
Save edobez/93e578d5ab81feb5dccdcee8194145ae to your computer and use it in GitHub Desktop.
How to flatten a nested list
l = [ [1, 2], [3, 4, 5] ]
# Option 1
# Nice because it works not only with lists but all kinds of iterators
flattened1 = list(itertools.chain(*l))
# Option 2
flattened2 = sum(l, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment