0

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

HashTagG
  • 15
  • 1
  • 5

3 Answers3

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