Python program to swap two elements in a list

Python program to swap two elements in a list

Given a list in Python and provided the positions of the elements, write a program to swap the two elements in the list.
Examples:

```Input : List = [23, 65, 19, 90], pos1 = 1, pos2 = 3
Output : [19, 65, 23, 90]

Input : List = [1, 2, 3, 4, 5], pos1 = 2, pos2 = 5
Output : [1, 5, 3, 4, 2]```

Approach #1: Simple swap, using comma assignment
Since the positions of the elements are known, we can simply swap the positions of the elements.

 `# Python3 program to swap elements` `# at given positions` `# Swap function` `def` `swapPositions(``list``, pos1, pos2):` `    ` `    ``list``[pos1], ``list``[pos2] ``=` `list``[pos2], ``list``[pos1]` `    ``return` `list` `# Driver function` `List` `=` `[``23``, ``65``, ``19``, ``90``]` `pos1, pos2  ``=` `1``, ``3` `print``(swapPositions(``List``, pos1``-``1``, pos2``-``1``))`

Output:

`[19, 65, 23, 90]`

Approach #2 : Using Inbuilt list.pop() function
Pop the element at pos1 and store it in a variable. Similarly, pop the element at pos2 and store it in another variable. Now insert the two popped element at each other’s original position.

 `# Python3 program to swap elements` `# at given positions` `# Swap function` `def` `swapPositions(``list``, pos1, pos2):` `    ` `    ``# popping both the elements from list` `    ``first_ele ``=` `list``.pop(pos1)  ` `    ``second_ele ``=` `list``.pop(pos2``-``1``)` `   ` `    ``# inserting in each others positions` `    ``list``.insert(pos1, second_ele) ` `    ``list``.insert(pos2, first_ele) ` `    ` `    ``return` `list` `# Driver function` `List` `=` `[``23``, ``65``, ``19``, ``90``]` `pos1, pos2  ``=` `1``, ``3` `print``(swapPositions(``List``, pos1``-``1``, pos2``-``1``))`

Output:

`[19, 65, 23, 90]`

Approach #3 : Using tuple variable
Store the element at pos1 and pos2 as a pair in a tuple variable, say get. Unpack those elements with pos2 and pos1 positions in that list. Now, both the positions in that list are swapped.

 `# Python3 program to swap elements at` `# given positions` `# Swap function` `def` `swapPositions(``list``, pos1, pos2):` `    ``# Storing the two elements` `    ``# as a pair in a tuple variable get` `    ``get ``=` `list``[pos1], ``list``[pos2]` `     ` `    ``# unpacking those elements` `    ``list``[pos2], ``list``[pos1] ``=` `get` `     ` `    ``return` `list` `# Driver Code` `List` `=` `[``23``, ``65``, ``19``, ``90``]` `pos1, pos2  ``=` `1``, ``3` `print``(swapPositions(``List``, pos1``-``1``, pos2``-``1``))`

Output:

`[19, 65, 23, 90]`

Last Updated on August 30, 2021 by admin

string.punctuation in Pythonstring.punctuation in Python

string.punctuation in Python In Python3, string.punctuation is a pre-initialized string used as string constant. In Python, string.punctuation will give

Python OpenCV – cv2.cvtColor() methodPython OpenCV – cv2.cvtColor() method

Python OpenCV | cv2.cvtColor() method OpenCV-Python is a library of Python bindings designed to solve computer

Basic calculator program using PythonBasic calculator program using Python

Basic calculator program using Python Create a simple calculator which can perform basic arithmetic operations

Initialize an Empty Dictionary in PythonInitialize an Empty Dictionary in Python

Initialize an Empty Dictionary in Python Dictionary in Python is an unordered collection of data values,

Python String find() methodPython String find() method

Python String find() method The find() method returns the lowest index of the substring if

How to print exception stack trace in Python?How to print exception stack trace in Python?

How to print exception stack trace in Python? To print stack trace for an exception

stdev() method in Python statistics modulestdev() method in Python statistics module

stdev() method in Python statistics module Statistics module in Python provides a function known as stdev() , which

Dunder or magic methods in PythonDunder or magic methods in Python

Dunder or magic methods in Python Dunder or magic methods in Python are the methods having two