The following code is available (demo)
f=lambda m, x:m and(x&1 and m.pop(0)or m.pop(0)[::-1])+f(m, x+1)
print(f([[4, 3, 2, 1], [5, 6, 7, 8], [12, 11, 10, 9], [13, 14, 15, 16]],0))
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
Here there is logic:
m.pop(0) if x&1 else m.pop(0)[::-1]
Please explain why when folding to the following view, is the code not executing correctly?
[m.pop(0)[::-1],m.pop(0)][x&1]
I don't know much about Python, will be grateful for any help, thank you.
UPD: If I change the logic, I get this result:
f=lambda m,x:m and([m.pop(0)[::-1],m.pop(0)][x&1])+f(m,x+1)
print(f([[4, 3, 2, 1], [5, 6, 7, 8], [12, 11, 10, 9], [13, 14, 15, 16]],0))
# [1, 2, 3, 4, 13, 14, 15, 16]
PS. Code-golf (If this is important, the essence of the code is that it bypasses the two-dimensional array in the form of a snake)
Solution:
[m.pop(0)[::-1],m.pop(0)][x&1]
=> (lambda: m.pop(0)[::-1], lambda: m.pop(0))[x&1]()
.