Rearrange a python list into n lists, by column

Posted by Ben R on Stack Overflow See other posts from Stack Overflow or by Ben R
Published on 2012-10-03T09:19:16Z Indexed on 2012/10/03 9:37 UTC
Read the original article Hit count: 222

Filed under:

Trying to solve this at this hour has gotten my mind into a tail-spin: I want to rearrange a list l into a list of n lists, where n is the number of columns.

e.g.,

l = [1,2,3,4,5,6,7,8] 
n = 5
==> [[1,6][2,7][3,8][4][5]]

another example:

l = [1,2,3,4,5,6,7,8,9,10]
n = 4
==> [[1,5,9],[2,6,10],[3,7][4,8]

Can someone please help me out with an algorithm? Feel free to use any python awesomeness that's available; I'm sure theres some cool mechanism that's a good fit for this, i just can't think of it.

© Stack Overflow or respective owner

Related posts about python