Created
September 15, 2014 19:39
-
-
Save gr33ndata/4815beb071f99976d4fd to your computer and use it in GitHub Desktop.
Flatten a List
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
# Let's have a list x = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] | |
# How to convert it into x = [0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4] | |
x = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] | |
# We thuse use reduce. | |
# When given a list, it goes throgu each pair of items from left to right, | |
# and applied some function on them. | |
# Remember, [1,3] + [2,4] => [1, 3, 2, 4] | |
reduce(lambda i,j: i+j , x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment