# How To Swap Two List Elements In Python

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.

## Swapping Two Elements of List using the Third variable in Python

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:

1. Initialize a list je_arr

2. Get user input into je_pos1, je_pos2 variables

3. Declare the third variable je_temp

4. Assign je_arr[je_pos1] to je_temp

5. Assign je_arr[je_pos2] to Je_arr[je_pos1]

6. Assign je_temp to je_arr[je_pos2]

7. Display new list

The je_pos1 and je_pos2 are positions of the elements which need to be swapped.

## Python Code:

``````# 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.

## Swapping Two Elements of List using the indexing in Python

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:

1. Initialize a list je_arr

2. Get user input to je_pos1 and je_pos2 variables

3. Swap the values at position je_pos1, je_pos2

4. Display new list.

## Python Code:

``````# 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]

## Swapping Two Elements of List using pop() function in Python

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.

## Python Code:

``````# 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]

Trending