I have 4 variables (1,2,3,4) and I have to write a Python code to move the values stored in those variables to the left, with the leftmost value ending up in the rightmost variable enter image description here
Asked
Active
Viewed 682 times
3 Answers
1
lis = [1,2,3,4]
lis = lis[1:] + [lis[0]]
A good description of slicing can be found here

Alec McKay
- 135
- 7
0
You can create a new list and copy values at the right place:
previous_list = [1,2,3,4]
new_list = []
for i in range(1, len(previous_list)):
new_list.append(previous_list[i])
new_list.append(previous_list[0])
print(new_list)

Victor Deleau
- 875
- 5
- 17
0
Try:
x=[1,2,3,4]
y=(x+x)[1:len(x)+1]
print(y)
Output:
[2, 3, 4, 1]
[Program finished]

Grzegorz Skibinski
- 12,624
- 2
- 11
- 34
-
1
-
1```(x+x)``` duplicates ```x``` so we have: ```[1,2,3,4,1,2,3,4]``` then iterating from 1 till 5 (```len(X)+1```) returns 3 elements from first ```x``` and 1 from the second. – Grzegorz Skibinski Nov 09 '19 at 22:26
-
1