Swap Elements of a List in Python

Swap Elements of a List in Python

  1. Use the Assignment Operator to Swap Elements of a List in Python
  2. Use the Third Variable to Swap Elements of a List in Python
  3. Use the pop() Function to Swap Elements of a List in Python

A list is a mutable (changeable) data structure in Python that stores an ordered collection of items. In this article, we will look at a few different ways to swap the elements of a list.

Use the Assignment Operator to Swap Elements of a List in Python

One of the easiest and most commonly used methods to swap a list of elements is through the assignment operator and comma.

In the following code, we have created a list and exchanged the values of index 1 with index 3 using the assignment operator that will assign the corresponding values from the right side of the assignment operator to the left variables.

Example Code:

#Python 3.x
list = [6, 2, 7, 8]
print('list before swapping:', list)
list[1], list[3] = list[3], list[1]
print('list after swapping:', list)

Output:

#Python 3.x
list before swapping: [6, 2, 7, 8]
list after swapping: [6, 8, 7, 2]

Using the assignment operator, we can swap the values of only two variables at a time. If we want to exchange multiple values, we can do it using a loop.

In the following code, the variables i and j will hold the index of the elements to swap. The values of indexes 0 and 3 will exchange in the first iteration, and the elements of indexes 4 and 6 will switch in the second iteration.

Example Code:

#Python 3.x
list = [6, 2, 7, 8, 5, 9, 10, 3, ]
print('list before swapping:', list)
for i,j in [(0,3),(4,6)]:
    list[i], list[j] = list[j], list[i]
print('list after swapping:', list)

Output:

#Python 3.x
list before swapping: [6, 2, 7, 8, 5, 9, 10, 3]
list after swapping: [8, 2, 7, 6, 10, 9, 5, 3]

Use the Third Variable to Swap Elements of a List in Python

We always need a third variable if we do not swap elements directly using the first approach. The third variable will temporarily hold the value of an index because we will lose it in the actual index after swapping.

In the following code, we have assigned the value of index 1 to the temp variable and index 3 to index 1. Then we have assigned the value of temp (stored value of index 1) to index 3.

Example Code:

#Python 3.x
list = [6, 2, 7, 8]
print('list before swapping:', list)
temp = list[1]
list[1] = list[3]
list[3] = temp
print('list after swapping:', list)

Output:

#Python 3.x
list before swapping: [6, 2, 7, 8]
list after swapping: [6, 8, 7, 2]

Use the pop() Function to Swap Elements of a List in Python

The pop() function with a list removes and returns the value from a specified index. In the following code, we have popped two elements from the list using their index and stored the returned values into two variables.

An important thing here is that we have used index 1 to remove the value 2, but we have used index 2 to clear the value 8. Because after we pop an item from the list, it will have a total of three elements.

So the index of 8 will be 2. Finally, we have inserted these values again in the list by specifying the indexes in reverse order.

Example Code:

#Python 3.x
list = [6, 2, 7, 8]
print('list before swapping:', list)
val1 = list.pop(1)
val2 = list.pop(2)
list.insert(1, val2)
list.insert(2, val1)
print('list after swapping:', list)

Output:

#Python 3.x
list before swapping: [6, 2, 7, 8]
list after swapping: [6, 8, 7, 2]
Author: Fariba Laiq
Fariba Laiq avatar Fariba Laiq avatar

I am Fariba Laiq from Pakistan. An android app developer, technical content writer, and coding instructor. Writing has always been one of my passions. I love to learn, implement and convey my knowledge to others.

LinkedIn

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python
  • What Is the Difference Between List Methods Append and Extend
  • Convert a List to String in Python