Python Program to Swap Two Elements in a List [4 Methods]

Swapping two elements in Python from a list refers to exchanging their positions. In other words, if we want to take two elements at specific indices in the list and swap their places. So that we can change the position of the elements within the list.

In this Python tutorial, I will demonstrate how to write a Python Program to swap two elements in a list.

Here, we will cover different methods to swap two elements in a list in Python, such as using comma assignment, temporary variables, pop() function, and enumerate() function.

To better understand the problem, let’s consider an example. Suppose we have a list of numbers with elements [1, 2, 3, 4, 5], and we want to swap the elements at indices 1 and 3.

After swapping, the updated list should be [1, 4, 3, 2, 5], where the element at index 1 (which is 2) is swapped with the element at index 3 (which is 4).

Let’s begin to implement these methods one by one with various examples.

Swap Two Elements in a List in Python Using Comma Assignment

We can swap the positions of the elements because the positions of the elements are known.

Here is the complete Python program to swap two elements in a list using a comma assignment.

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 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 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]

Refer to the below screenshot, it provides the output of the Python program to swap two elements in a list using comma assignment.

Swap two elements in a list in Python

Swap Two Elements in a List Using the Temp Variable

We will see how to swap two elements in a Python list using a temp variable. This is the source code of Python to swap two elements in a list using a temp variable.

We will use a temp variable to store the element’s values in Python.

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]

The image below shows the output of the Python program to swap two elements in a list using the temp variable.

Python program to swap two elements in a list

Swap Two Elements in a List Using the Enumerate Function

In Python, enumerate is a built-in function that adds a counter to an iterable and returns it as an enumerate object (iterator with index and the value).

Here is the complete Python program to swap two elements in a list using the enumerate function.

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]

You can refer to the screenshot below to see the output of the Python program to swap two elements in a list using the enumerate function.

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

Python Program to Swap Two Elements in a List Using 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]

Refer to the screenshot below; it provides the output of the Python program to swap two elements in a list using the pop() function in Python.

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

Conclusion

In this post, we have shown 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 we 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: