swap list elements in python

Swap Elements in a Python List with these Methods

In this tutorial, we will look at how to swap two elements in a Python list with the help of some examples.

swap list elements in python

Let’s say you have a list, ls and you’d like to swap the elements at indices i and j. To do so, you can use the following methods –

  1. Using a temp variable

    • Assign the value of ls[i] to the temp variable.
    • Change the value of ls[i] to ls[j].
    • Assign the value of temp to ls[j].
  2. Without using a temp variable
    • In Python, we can also dynamically two swap values without using a third variable, use the syntax ls[i], ls[j] = ls[j], ls[i] to swap the two values.

Examples

Let’s now look at some use cases of swapping list elements with the help of some examples.

Swap two list elements using a temp variable

Let’s say we have a list of numbers and we want to swap the values at the i and j indices.

# create a list
ls = [1, 2, 3, 4, 5]
# indices of values to swap
i = 1
j = 3
# swap the values
temp = ls[i]
ls[i] = ls[j]
ls[j] = temp
# display the list
print(ls)

Output:

[1, 4, 3, 2, 5]

Here, we used a temporary variable to swap two values in a list. You can see that the values at indices 1 and 3 have been swapped.

Swap two list elements without using a temp variable

Let’s take the same task as above but this time do it without using a temp variable.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# create a list
ls = [1, 2, 3, 4, 5]
# indices of values to swap
i = 1
j = 3
# swap the values
ls[i], ls[j] = ls[j], ls[i]
# display the list
print(ls)

Output:

[1, 4, 3, 2, 5]

We get the same result as above and didn’t need to use an additional variable to perform the swap operation.

Swap the first and the last list elements

We can use the above syntax to swap two list elements at any index. To swap the first and the last list elements, use the index 0 (for the first element) and length-1 (for the last element).

# create a list
ls = [1, 2, 3, 4, 5]
# indices of values to swap
i = 0
j = len(ls)-1
# swap the values
ls[i], ls[j] = ls[j], ls[i]
# display the list
print(ls)

Output:

[5, 2, 3, 4, 1]

The first and the last elements have been swapped.

Swap list elements by value

What if you only know the values to be swapped and not their indices?
In that case, use the Python list index() function to first get the indices of the values to be swapped and then continue with the above method.

For example, in a list of numbers from 1 to 5, let’s swap 3 and 5.

# create a list
ls = [1, 2, 3, 4, 5]
# get the indices of values to swap
i = ls.index(3)
j = ls.index(5)
# swap the values
ls[i], ls[j] = ls[j], ls[i]
# display the list
print(ls)

Output:

[1, 2, 5, 4, 3]

Values 3 and 5 have been swapped.

FAQs

How can I swap two elements in a list in Python?

You can use the following syntax to swap two elements (at indices i and j) in a list:
list[i], list[j] = list[j], list[i].

Is it possible to swap elements in a list that are in a nested list?

Yes, you can use the same syntax and methodology as above to swap elements in a nested list. You just need to specify the indexes of the elements in the nested list correctly.

How can I swap elements in a list if I only know their values and not their indexes?

You can use the list index() method to find the indices of the elements you want to swap, and then use the syntax above to swap them.

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top