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)
▶ RUN

Output/ Explanation:

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

Original List is: [3, 4, 5, 6]

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

Latest Current Affairs 2025 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