In this tutorial, we will learn how to swap elements in a list using Python. We’ll cover three different methods: a function, direct swapping of the first and last elements, and swapping using the pop() and insert() methods.
Method 1: Swap Two Elements in a List using Function
Here are the steps to write a Python Python Tutorial program to swap any two elements in a list using a function:
- Declare an empty list.
- Take the number of elements in the list and store it in a variable.
- Use a for loop to take value one by one and insert them into the list using the append() method.
- Accept input values from the user, which they want to swap in the list.
- Define a function and use this formula list[pos1], list[pos2] = list[pos2], list[pos1] to swap the values.
- Call the function and print the result.
Here’s the code:
NumList = [] # get number of elements Number = int(input("How many elements in list: ")) # fill list with user input for i in range(1, Number + 1): value = int(input("Enter the value of element %d: " % i)) NumList.append(value) # print list before swapping print("List before swapping:", NumList) # get positions of elements to swap position1 = int(input("Enter the position of the first element to swap: ")) position2 = int(input("Enter the position of the second element to swap: ")) # define swap function def swap_positions(list, pos1, pos2): list[pos1], list[pos2] = list[pos2], list[pos1] return list # call swap function and print result print("List after swapping:", swap_positions(NumList, position1-1, position2-1))
Method 2: Swap the First and Last Value of a List
Here are the steps to write a Python program to swap the first and last element in a list:
- Declare an empty list.
- Take the number of elements in the list and store it in a variable.
- Use a for loop to take value one by one and insert them into the list using the append() method.
- Swap the first and last element in the list using a temporary variable.
- Print the result.
Here’s the code:
NumList = [] # get number of elements Number = int(input("How many elements in list: ")) # fill list with user input for i in range(1, Number + 1): value = int(input("Enter the value of element %d: " % i)) NumList.append(value) # print list before swapping print("List before swapping:", NumList) # swap first and last element temp = NumList[0] NumList[0] = NumList[Number-1] NumList[Number-1] = temp # print list after swapping print("List after swapping:", NumList)
Method 3: Swap Two Elements in a List using Inbuilt POP() and Insert() Function
Here are the steps to write a Python program to swap any two elements in a list using the pop() and insert() methods:
- Declare an empty list.
- Take the number of elements in the list and store it in a variable.
- Use a for loop to take value one by one and insert them into the list using the append() method.
- Accept input values from the user, which they want to swap in the list.
- Define a function and use the list.pop(pos1) and list.insert(pos2) methods to swap the values.
- Call the function and print the result.
Here’s the code:
NumList = [] # get number of elements Number = int(input("How many elements in list: ")) # fill list with user input for i in range(1, Number + 1): value = int(input("Enter the value of element %d: " % i)) NumList.append(value) # print list before swapping print("List before swapping:", NumList) # get positions of elements to swap position1 = int(input("Enter the position of the first element to swap: ")) position2 = int(input("Enter the position of the second element to swap: ")) # define swap function def swap_list(list, pos1, pos2): x = list.pop(pos1) y = list.pop(pos2-1) list.insert(pos1, y) list.insert(pos2, x) return list # call swap function and print result print("List after swapping:", swap_list(NumList, position1-1, position2-1))
That’s it! Now you know how to swap elements in a list using Python.