Skip to content

Instantly share code, notes, and snippets.

@gr33ndata
Created September 15, 2014 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gr33ndata/4815beb071f99976d4fd to your computer and use it in GitHub Desktop.
Save gr33ndata/4815beb071f99976d4fd to your computer and use it in GitHub Desktop.
Flatten a List
# 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