Welcome folks today in this blog post we will be swapping
two numbers or elements inside the list using python. All the full source code of the application is shown below.
Get Started
In order to get started we need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 |
def swapPositions(list, pos1, pos2): list[pos1], list[pos2] = list[pos2], list[pos1] return list # Driver function List = [23, 65, 19, 90] pos1 = int(input("Enter the first position")) pos2 = int(input("Enter the second position")) print(swapPositions(List, pos1-1, pos2-1)) |
So here we are taking the input from user that is the positions
inside the list to swap so if you execute the above script by typing the below command
python app.py