Home »
Python »
Python Programs
Python program to swap any two elements in the list
Here, we are going to learn how to swap any two elements in the list in Python?
Submitted by Shivang Yadav, on April 06, 2021
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].
Swap two elements in a list using the index
We will take a list and two indexes (for swapping ) from the user and then swap the values at the given index in the list.
Example
Sample Input:
[3, 1, 0, 9, 23, 89, 10] , 2, 5
Output:
[3, 1, 89, 9, 23, 0, 10]
Method 1: Simply swap values using comma separator
We will swap the elements directly using the common swapping method.
Syntax
a, b = b, a
Program to swap any two elements in the list using comma separator
# Python program to swap element of a list
# Getting list from user
myList = []
length = int(input("Enter number of elements: "))
for i in range(0, length):
val = int(input())
myList.append(val)
print("Enter indexes to be swapped ")
index1 = int(input("index 1: "))
index2 = int(input("index 2: "))
print("Initial List: ", myList)
# Swapping given elements
myList[index1], myList[index2] = myList[index2], myList[index1]
# Printing list
print("List after Swapping: ", myList)
The output of the above program is:
Enter number of elements: 5
10
20
30
40
50
Enter indexes to be swapped
index 1: 2
index 2: 4
Initial List: [10, 20, 30, 40, 50]
List after Swapping: [10, 20, 50, 40, 30]
Method 2: Pop values and then inserting them back
One more method to swap values is to pop out values at a given index and then insert them back to the given index.
Algorithm
- Get list and index values from the user.
- Swap values using pop and insert method.
- val1 = pop(index1)
- val2 = pop(index2 - 1)
- insert(index1, val2)
- insert(index2, val1)
- Print the list.
Program to swap any two elements in the list
# Python program to swap element of a list
# Getting list from user
myList = []
length = int(input("Enter number of elements "))
for i in range(0, length):
val = int(input())
myList.append(val)
print("Enter indexes to be swapped ")
index1 = int(input("index 1: "))
index2 = int(input("index 2: "))
print("Initial List: ", myList)
# Swapping given element
val1 = myList.pop(index1)
val2 = myList.pop(index2 - 1)
myList.insert(index1, val2)
myList.insert(index2, val1)
# Printing list
print("List after Swapping: ", myList)
The output of the above program is:
Enter number of elements 5
10
20
30
40
50
Enter indexes to be swapped
index 1: 1
index 2: 4
Initial List: [10, 20, 30, 40, 50]
List after Swapping: [10, 50, 30, 40, 20]
Swapping two values based on their values entered by the user
# Python program to swap element of a list
# Getting list from user
myList = []
length = int(input("Enter number of elements: "))
for i in range(0, length):
val = int(input())
myList.append(val)
print("Enter values to be swapped ")
value1 = int(input("value 1: "))
value2 = int(input("value 2: "))
index1 = myList.index(value1)
index2 = myList.index(value2)
print("Initial List: ", myList)
# Swapping given element
myList[index1], myList[index2] = myList[index2], myList[index1]
# Printing list
print("List after Swapping: ", myList)
The output of the above program is:
Enter number of elements: 5
10
20
30
40
50
Enter values to be swapped
value 1: 10
value 2: 50
Initial List: [10, 20, 30, 40, 50]
List after Swapping: [50, 20, 30, 40, 10]
Python List Programs »