CBSE Class 12 Computer Science Question 73 of 120

Review of Python Basics — Question 25

Back to all questions
25
Question

Question 20

Write a program to shift elements of a list so that the first element moves to the second index and second index moves to the third index, and so on, and the last element shifts to the first position.

Suppose the list is [10, 20, 30, 40]
After shifting, it should look like: [40, 10, 20, 30]
Solution
l = eval(input("Enter the list: "))
print("Original List")
print(l)

l = l[-1:] + l[:-1]

print("Rotated List")
print(l)
Output
Enter the list: [10, 20, 30, 40]
Original List
[10, 20, 30, 40]
Rotated List
[40, 10, 20, 30]
Answer