Python 3 Script to Swap or Interchange Two Elements or Position Input by User Inside the List Full Example For Beginners

Python 3 Script to Swap or Interchange Two Elements or Position Input by User Inside the List Full Example For Beginners

 

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

 

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

 

See also  Python 3 PyQt5 Taking User Input Dialog Using Popup Window in QInputDialog Full Tutorial For Beginners

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

 

Leave a Reply