In this tutorial, we will look at how to swap two elements in a Python list with the help of some examples.
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 –
Using a
temp
variable- Assign the value of
ls[i]
to thetemp
variable. - Change the value of
ls[i]
tols[j]
. - Assign the value of
temp
tols[j]
.
- Assign the value of
- 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.
- In Python, we can also dynamically two swap values without using a third variable, use the syntax
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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
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]
.
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.
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 –
- Python – Check If Index Exists in List
- Python List Sort – With Examples
- Python – Convert a deque to a list
- Python – Check If a List is Empty – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.