CBSE Class 11 Informatics Practices Question 31 of 33

Lists in Python — Question 31

Back to all questions
31
Question

Question 26

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

Suppose 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

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]