m=eval(input("enter number")) for i in range(0,len(m),2): m[i],m[i+1]= m[i+1],m[i] print("swapped list",m) #output enter number[1,2] swapped list [2, 1]
Here is what the above code is Doing:
1. We take input from the user and store it in a variable m.
2. We iterate over the list m using a for loop.
3. We swap the elements at index i and index i+1.
4. We print the swapped list.