Python Program To Swap Two Elements In A List [4 Methods] - Python Guides (2024)

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.

Table of Contents

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.

Python Program To Swap Two Elements In A List [4 Methods] - Python Guides (1)

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.

READ: How to Use Switch Case in Python with User Input[5 ways]

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 listList = [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 [4 Methods] - Python Guides (2)

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, 2print(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.

Python Program To Swap Two Elements In A List [4 Methods] - Python Guides (3)

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 listList = [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.

Python Program To Swap Two Elements In A List [4 Methods] - Python Guides (4)

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.

READ: How to Convert a String to a Double in Python [7 Ways]

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:

  • How to Swap Two Numbers in Python Using Function
  • Python Program to Check Armstrong Number
  • Python Program for Selection Sort

Python Program To Swap Two Elements In A List [4 Methods] - Python Guides (5)

Bijay Kumar

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.

Python Program To Swap Two Elements In A List [4 Methods] - Python Guides (2024)

References

Top Articles
Latest Posts
Article information

Author: Nathanael Baumbach

Last Updated:

Views: 6685

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Nathanael Baumbach

Birthday: 1998-12-02

Address: Apt. 829 751 Glover View, West Orlando, IN 22436

Phone: +901025288581

Job: Internal IT Coordinator

Hobby: Gunsmithing, Motor sports, Flying, Skiing, Hooping, Lego building, Ice skating

Introduction: My name is Nathanael Baumbach, I am a fantastic, nice, victorious, brave, healthy, cute, glorious person who loves writing and wants to share my knowledge and understanding with you.