To swap elements of a List in Python. We can use several ways such as swapping using two or three variables or using the indexing approach. We will discuss several different approaches here.
Let's start with some examples.
We can swap two elements in a list by using a third variable that acts as a temporary holder variable.
Firstly, store the value of the first variable in the third variable, then store the value of the second variable in the first variable. Finally, store the value of the third variable in the second variable. Look at the algorithm to understand this approach better:
Initialize a list je_arr
Get user input into je_pos1, je_pos2 variables
Declare the third variable je_temp
Assign je_arr[je_pos1] to je_temp
Assign je_arr[je_pos2] to Je_arr[je_pos1]
Assign je_temp to je_arr[je_pos2]
Display new list
The je_pos1 and je_pos2 are positions of the elements which need to be swapped.
# Initialise a list
je_arr = [ 1, 3, 43, 66, 321, 4 ]
# input je_pos1, je_pos2
je_pos1 = int(input("Enter first position"))
je_pos2 = int(input("Enter second position"))
# print the list
print("Before Swapping")
print(je_arr)
# swap using third variable
je_temp = je_arr[je_pos1]
je_arr[je_pos1] = je_arr[je_pos2]
je_arr[je_pos2] = je_temp
# print the list
print("After Swapping")
print(je_arr)
Output:
Enter first position2
Enter second position3
Before Swapping
[1, 3, 43, 66, 321, 4]
After Swapping
[1, 3, 66, 43, 321, 4]
An important point to remember is that indexing starts from 0.
In python, we can swap two variables directly as follows. Actully, here the expression (a,b) is a tuple and we are swaping using tuple elements by assignment.
a,b = b,a
Using this approach, we will swap two elements in a list. Look at the algorithm below:
Initialize a list je_arr
Get user input to je_pos1 and je_pos2 variables
Swap the values at position je_pos1, je_pos2
Display new list.
# Initialise a list
je_arr = [ 1, 3, 43, 66, 321, 4 ]
# input je_pos1, je_pos2
je_pos1 = int(input("Enter first position"))
je_pos2 = int(input("Enter second position"))
# print the list
print("Before Swapping")
print(je_arr)
# swap using Indexing
je_arr[je_pos1], je_arr[je_pos2] = je_arr[je_pos2], je_arr[je_pos1]
# print the list
print("After Swapping")
print(je_arr)
Output:
Enter first position 1
Enter second position 5
Before Swapping
[1, 3, 43, 66, 321, 4]
After Swapping
[1, 4, 43, 66, 321, 3]
In this approach, we use of the pop() and insert() methods of the list to swap two elements in a list. We first pop the element as position 1 and store it in some variable, say pop1. We now have n-1 elements in the list. We now pop the element at position 2 and store it in pop2.
Since the number of elements in the list has decreased therefore the second element is now present at position2 -1. We then insert pop1 at pos2 and pop2 at pos1. See the Python code below.
# Initialise a list
je_arr = [ 1, 3, 43, 66, 321, 4 ]
# input je_pos1, je_pos2
je_pos1 = int(input("Enter first position"))
je_pos2 = int(input("Enter second position"))
# print the list
print("Before Swapping")
print(je_arr)
# swap using Indexing
je_pop1 = je_arr.pop(je_pos1)
je_pop2 = je_arr.pop(je_pos2 - 1)
je_arr.insert(je_pos2 -1, je_pop1)
je_arr.insert(je_pos1, je_pop2)
# print the list
print("After Swapping")
print(je_arr)
Output:
Enter first position 0
Enter second position 1
Before Swapping
[1, 3, 43, 66, 321, 4]
After Swapping
[3, 1, 43, 66, 321, 4]