Efficiently exchange elements between two lists
list[[All, 2 ;; ;; 2]] = Reverse @ list[[All, 2 ;; ;; 2]];
list
{{1, 7, 3, 9, 5}, {6, 2, 8, 4, 10}}
or list[[-1 ;; 1 ;; -1, 2 ;; ;; 2]]
.
ps. keep in mind that this changes list
.
ClearAll[f1]
f1 = Module[{l2 = Transpose@#}, l2 = Transpose[MapAt[Reverse, l2, {2 ;; ;; 2}]]] &;
f1@{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}
{{1, 7, 3, 9, 5}, {6, 2, 8, 4, 10}}
Or
ClearAll[f2]
f2 = Module[{l2 = #},
l2[[All, 2 ;; ;; 2]] = Reverse /@ Transpose[l2[[All, 2 ;; ;; 2]]]; l2] &;
f2@{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}}
{{1, 7, 3, 2, 5}, {6, 9, 8, 4, 10}}
I think Reverse
is better suited for long lists. When the lists that need be reversed have length 2, I would do this:
L = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
Transpose[MapAt[#[[{2, 1}]] &, Transpose[L], 2 ;; ;; 2]]