To shift values in a list (replace the value at the first place of the list to its last place and vice versa), you need to rearrange the list with two slices.
The shift for the list:
1 |
lst = [1,2,3,4,5] |
forward:
1 2 3 4 |
lst = lst[1:] + lst[:1] print(lst) [2, 3, 4, 5, 1] |
and backward:
1 2 3 4 |
lst = lst[-1:] + lst[:-1] print(lst) [1, 2, 3, 4, 5] |
If a cyclic shift is necessary, repeat this command the required number of times in “for” cycle.