Recently, I was working on a data-cleaning project where I had to reorder elements in a customer list.
The challenge was simple: swap two items in a Python list.
At first, I thought there might be a single “built-in” function to do this. But as I explored, I realized there are multiple ways to swap elements in Python- some quick and elegant, others more explicit and beginner-friendly.
In this tutorial, I’ll show you four different methods I use to swap two elements in a Python list. Each method comes with a working example so you can try it right away.
Methods to Swap Two Elements in a Python List
Let me explain to you the methods to swap two elements in a Python list.
1. Use Comma Assignment
We can swap the positions of the elements because the positions of the elements are known.
def swap_positions(list, position1, position2):
list[position1], list[position2] = list[position2], list[position1]
return list
list_of_elements = [30, 56, 19, 87]
position1, position2 = 1, 4
print(swap_positions(list_of_elements, position1-1, position2-1))Here, I have used a comma assignment in this Python program to perform simultaneous assignments or swapping of elements in a list.
list[position1], list[position2] = list[position2], list[position1]Here, we call a Python function named swap_position with an arguments list_of_elements, position1-1, and position2-1. The -1 adjusts the positions since indexing typically starts from 0.
print(swap_positions(list_of_elements, position1-1, position2-1))Output:
[87, 56, 19, 30]I executed the above example code and added the screenshot below.

Comma assignment allows you to easily swap elements in a list by performing simultaneous value assignments in a single line of code.
2. Use the Temp Variable
This example demonstrates swapping elements in a Python list using a temporary variable to hold one of the values during the swap.
def swap_elements(list, order1, order2):
temp=list[order1]
list[order1]=list[order2]
list[order2]=temp
return list
List = [43, 15, 59, 99]
order1, order2 = 1, 3
print(swap_elements(List, order1-1, order2-1))Output:
[59, 15, 43, 99]I executed the above example code and added the screenshot below.

By using a temporary variable, elements can be swapped safely and effectively without overwriting values.
3. Use the Enumerate Function
The enumerate is a built-in function in Python that adds a counter to an iterable and returns it as an enumerate object (iterator with index and the value).
def swapElements_order(list, position1, position2):
for i, x in enumerate(list):
if i == position1:
elem1 = x
if i == position2:
elem2 = x
list[position1] = elem2
list[position2] = elem1
return list
List = [123, 65, 119, 90]
position1, position2 = 0, 2
print(swapElements_order(List, position1, position2))Here, using the enumerate function to iterate over the elements of the list and by using conditional if statements in Python, we will check if the current index is equal to the value of the specified position.
for i, x in enumerate(list):
if i == position1:
elem1 = x
if i == position2:
elem2 = xOutput:
[119, 65, 123, 90]I executed the above example code and added the screenshot below.

Using enumerate() makes it easy to access both index and value, allowing precise element swaps in a list based on specified positions.
4. Use the Pop Function
The pop() function in Python is used to remove the element at the specified position. This is the complete Python code to swap two elements in a list using the pop function.
def swap_positions(list, order1, order2):
first_ele = list.pop(order1)
second_ele = list.pop(order2-1)
list.insert(order1, second_ele)
list.insert(order2, first_ele)
return list
List = [23, 65, 19, 90]
order1, order2 = 1, 3
print(swap_positions(List, order1-1, order2-1))The pop() function is used here to remove and return the element at the specified index in Python. Since indices typically start from 0, -1 adjusts the order’s index.
first_ele = list.pop(order1)
second_ele = list.pop(order2-1)Output:
[29, 75, 13, 40]I executed the above example code and added the screenshot below.

Using pop() and insert() methods in Python allows swapping elements by temporarily removing them and reinserting them at their desired positions.
In this post, I have shown you how to write a Python Program to swap two elements in a list. Additionally, we have discussed the different approaches for swapping two elements in Python and provided a step-by-step explanation of each example.
Methods I explained in this Python tutorial to swap two elements in a list in Python include comma assignment, temporary variables, enumerate() function, and pop() function.
You may also like to read:
- Count Occurrences in Python List
- Remove Multiple Items From a List in Python
- Remove Brackets From List in Python
- ValueError: Could Not Convert String to Float in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.