Programming Examples

Python program to rotate list elements with next elements


Write a program rotates the elements of a list so that the elements at the first index moves to the second index , the element in the second index moves to the third index, etc., and the element at the last index moves to the first index.

Solution

lst=eval(input("Enter a list : "))
print("List before Rotation :",lst)
a=lst[0]
s=len(lst)
for i in range(1,s):
    lst[i-1]=lst[i]
lst[s-1]=a
print("List after Rotate :",lst)
Output/ Explanation:

Enter a list : [3,4,5,6]

List before Rotation : [3, 4, 5, 6]

List after Rotate : [4, 5, 6, 3]

Latest Current Affairs 2024 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad