If I have a list:
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3]
My goal is to split it into equal sized pieces n, undo each piece, and then return the pieces in order. So, for the example above, for the 4th piece, I get:
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3]
[_________] [_________] [________] [______]
| | | |
1 2 3 4 (this is smaller than 4 but receives the same treatment)
||
[4, 3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 3, 2, 1]
This is what I have:
n = 4
l = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3]
chunks = [l[i : i + n] for i in range(0, len(l), n)]
print(chunks)
for i in range(len(chunks)):
chunks[i] = list(reversed(chunks[i]))
from functools import reduce
out = list(reduce(lambda x, y: x + y, chunks))
print(out)
I do not think this is very good. Is there any other way that uses python libraries better than this?