Skip to content

Instantly share code, notes, and snippets.

@dagg
Last active January 6, 2020 00:12
Show Gist options
  • Save dagg/204a5ef9b6f7ccdd5091 to your computer and use it in GitHub Desktop.
Save dagg/204a5ef9b6f7ccdd5091 to your computer and use it in GitHub Desktop.
List Flattening Methods
import itertools
from functools import reduce
l = [[1,2,3],[4,5,6],[7],[8,9]]*99
#(1)
list(itertools.chain.from_iterable(l))
#(2)
reduce(lambda x,y: x+y, l)
#(3)
[item for sublist in l for item in sublist]
#(4)
sum(l, [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment