Python Program to Swap Two Elements in a List

Python Program to Swap Two Elements in a List

Python program to swap any two elements in the list; In this tutorial, you will learn how to swap any two elements in the list in python program.

Python Program to Swap Two Elements in List

  • Python Program to Swap Two Elements in a List using Function.
  • Python Program to Swap the First and Last Value of a List.
  • Python Program to Swap Two Elements in a List using Inbuilt POP() and Insert() Function.

Python Program to Swap Two Elements in a List using Function

Use the following steps to write a python program to swap any two elements in the list:

  • First of all, declare a number list.
  • Take the number of elements in the list and store it in a variable.
  • Iterate for loop to take value one by one and insert them into the list using append() method.
  • Accept input values, which user want to swap in list.
  • Define function and use this formula list[pos1], list[pos2] = list[pos2], list[pos1] to swap value
  • Call function and print result.
# Python Program to Swap Two Elements in a List using Function
     
NumList = []

#how many elements in list
Number = int(input("How many elements in list :- "))
    
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element :- " %i))
    NumList.append(value)

#print list before swapping
print("\nList before swapping of elements :-\n",NumList)

#take position to swap
position1 = int(input("Enter the position 1 of element, which you want to swap :- "))
position2 = int(input("Enter the position 1 of element, which you want to swap :- "))

# Swap function 
def swapPositions(list, pos1, pos2): 
      
    list[pos1], list[pos2] = list[pos2], list[pos1] 
    return list
  
print("List after swapping of elements :-\n",swapPositions(NumList, position1-1, position2-1)) 

After executing the program, the output will be:

How many elements in list :-  6
Please enter the Value of 1 Element :-  1
Please enter the Value of 2 Element :-  2
Please enter the Value of 3 Element :-  3
Please enter the Value of 4 Element :-  4
Please enter the Value of 5 Element :-  5
Please enter the Value of 6 Element :-  6

List before swapping of elements :-
 [1, 2, 3, 4, 5, 6]
Enter the position 1 of element, which you want to swap :-  3
Enter the position 1 of element, which you want to swap :-  6
List after swapping of elements :-
 [1, 2, 6, 4, 5, 3]

2: Python Program to Swap the First and Last Value of a List

Use the following steps to write a python program to swap the first and last element in the list:

  • First of all, declare a number list.
  • Take the number of elements in the list and store it in a variable.
  • Iterate for loop to take value one by one and insert them into the list using append() method.
  • Next, swap first and the last element in the list.
  • Print the result.
# Python Program to Swap the First and Last Value of a List
     
NumList = []

#how many elements in list
Number = int(input("How many elements in list :- "))
    
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element :- " %i))
    NumList.append(value)

#print list before swapping
print("\nList before swapping of elements :-\n",NumList)

# Swap list 
temp=NumList[0]
NumList[0]=NumList[Number-1]
NumList[Number-1]=temp
  
print("List after swapping of elements :-\n",NumList) 

After executing the program, the output will be:

How many elements in list :-  5
Please enter the Value of 1 Element :-  5
Please enter the Value of 2 Element :-  4
Please enter the Value of 3 Element :-  3
Please enter the Value of 4 Element :-  2
Please enter the Value of 5 Element :-  1

List before swapping of elements :-
 [5, 4, 3, 2, 1]
List after swapping of elements :-
 [1, 4, 3, 2, 5]

3: Python Program to Swap Two Elements in a List using Inbuilt POP() and Insert() Function.

Use the following steps to write a python program to swap any two elements in the list using pop and insert() function:

  • First of all, declare a number list.
  • Take the number of elements in the list and store it in a variable.
  • Iterate for loop to take value one by one and insert them into the list using append() method.
  • Accept input values from user, which user want to swap in list.
  • Define function and use list.pop(pos1) and list.insert(post2) to swap value
  • Call function and print result.
# Python Program to Swap the First and Last Value of a List
     
NumList = []

#how many elements in list
Number = int(input("How many elements in list :- "))
    
for i in range(1, Number + 1):
    value = int(input("Please enter the Value of %d Element :- " %i))
    NumList.append(value)

#print list before swapping
print("\nList before swapping of elements :-\n",NumList)

#take position to swap
position1 = int(input("Enter the position 1 of element, which you want to swap :- "))
position2 = int(input("Enter the position 1 of element, which you want to swap :- "))

# Swap function
def swapList(list, pos1, pos2): 
      
    # popping both the elements from list 
    x = list.pop(pos1)    
    y = list.pop(pos2-1) 
     
    # inserting in each others positions 
    list.insert(pos1, y)   
    list.insert(pos2, x)   
      
    return list
 
print("List after swapping of elements :-\n",swapList(NumList, position1-1, position2-1)) 

After executing the program, the output will be:

How many elements in list :-  4
Please enter the Value of 1 Element :-  1
Please enter the Value of 2 Element :-  2
Please enter the Value of 3 Element :-  3
Please enter the Value of 4 Element :-  4

List before swapping of elements :-
[1, 2, 3, 4]
Enter the position 1 of element, which you want to swap :-  2
Enter the position 1 of element, which you want to swap :-  3
List after swapping of elements :-
[1, 3, 2, 4]

Recommended Python Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published.