Programming Examples
Python program to swap list elements with even and locations
write a program to input a list of numbers and swap elements at the even location with the elements at the odd location.
Solution
val=eval(input("Enter a list "))
print("Original List is:",val)
s=len(val)
if s%2!=0:
s=s-1
for i in range(0,s,2):
val[i],val[i+1]=val[i+1],val[i]
print("List after swapping :",val)
Output
Enter a list [3,4,5,6]
Original List is: [3, 4, 5, 6]
List after swapping : [4, 3, 6, 5]