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]

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz