This issue of perestroika arose earlier. But instead of searching, I will quickly demonstrate the numpy approach
create an array of samples:
In [473]: x=np.arange(20).reshape(2,10) In [474]: x Out[474]: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]])
Use reseape to break it into blocks of 5
In [475]: x.reshape(2,2,5) Out[475]: array([[[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9]], [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]])
and use transpose to resize and essentially reorder these lines
In [476]: x.reshape(2,2,5).transpose(1,0,2) Out[476]: array([[[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14]], [[ 5, 6, 7, 8, 9], [15, 16, 17, 18, 19]]])
and another form for consolidation of the 1st 2-dimensional size
In [477]: x.reshape(2,2,5).transpose(1,0,2).reshape(4,5) Out[477]: array([[ 0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [ 5, 6, 7, 8, 9], [15, 16, 17, 18, 19]])
If x already a numpy array, these transpose and change operations look cheap (in time). If x was really nested lists, then another solution with list operations would be faster since creating a numpy array has overhead.