Python Program to Swap Two Elements in a List

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.

Swap two elements in a list in Python

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.

Python program to swap two elements in a list

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 = x

Output:

[119, 65, 123, 90]

I executed the above example code and added the screenshot below.

Swap two elements in a list in Python using the enumerate function

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.

How to swap two elements in a list using pop() function in Python

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:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.