KnowledgeBoat Logo
|

Informatics Practices

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]

Python List Manipulation

6 Likes

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]

Answered By

3 Likes


Related Questions