Last active
January 6, 2020 00:12
-
-
Save dagg/204a5ef9b6f7ccdd5091 to your computer and use it in GitHub Desktop.
List Flattening Methods
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
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