Signup/Sign In

Python Program to Swap Two Elements in a List

In this tutorial, we will learn, how to swap two elements at given positions in a List. Swapping means exchanging the values of the two numbers. Previously, we learned how to swap the first and last elements of a list. This program is similar to it, the only difference is that the user will specify the position of the elements which have to be swapped. The program should return and display the swapped list.

Look at the examples to understand the input and output format.

Input : [1, 2, 8, 9, 3, 0, 7] pos1=2 pos2=4

Output : [1, 9, 8, 2, 3, 0, 7]

Approach

To execute this task we can follow multiple approaches, some are discussed below-

  1. By using a third variable for swapping the values present at the given indexes.
  2. By using direct swapping
  3. By using pop() function to pop the elements at the given position.

We will discuss all three approaches in detail separately.

Approach 1: Swapping using third variable

In this approach, we will use a third variable to swap the values present at the given position.

The logic behind swapping two numbers using a third variable is:

Store the value of the first variable in the third variable.

Store the value of the second variable in the first variable.

At last, store the value of the third variable in the second variable.

If, var1 is the first variable and var2 is the second variable and temp is the third variable which will hold the value of var1, the code for swapping is,

temp = var1;

var1 = var2;

var2 = temp;

Now, we will look at the algorithm to understand the implementation of the approach.

Algorithm

Step 1- Define a function to swap elements with the list sl and positions pos1 and pos2 as parameters

Step 2- Swap elements sl[pos1] and sl[pos2] using a third variable

Step 3- Return the swapped list

Step 4- Define the list values and the positions of the elements which have to be swapped

Step 5- Pass the list and (pos1-1) and (pos2-1) in the function (because indexing starts from 0) and print the result

Python Program 1

Look at the complete program given below for swapping elements at index 0 and index n-1.

#swap two elements in list

# Swap function
def swapList(sl,pos1,pos2):
    n = len(sl)     
    # Swapping 
    temp = sl[pos1]
    sl[pos1] = sl[pos2]
    sl[pos2] = temp
    return sl      

l= [10, 14, 5, 9, 56, 12]
pos1= 2
pos2= 5

print(l)
print("Swapped list: ",swapList(l,pos1-1,pos2-1))


[10, 14, 5, 9, 56, 12]
Swapped list: [10, 56, 5, 9, 14, 12]

Approach 2: Swapping using Indexing

In this approach, we will directly swap the values at the given index.

In Python, we can directly swap two variables a and b without using a third variable using the code- a, b = b, a

Look at the algorithm to understand the working of the program

Algorithm

Step 1- Define a function that will swap the values

Step 2- Swap the elements at index pos1 and pos2

Step 3- Return the swapped list

Step 4- Declare list values and give the position of the elements which have to be swapped

Step 5- Pass the list and the positions in the function and display the result

Python Program 2

Look at the complete program given below.

#swap two elements in list

# Swap function
def swapList(sl,pos1,pos2):      
    sl[pos1], sl[pos2] = sl[pos2], sl[pos1]  
    return sl
      
List = [9, 11, 5, 3, 6, 27, 4]
pos1, pos2= 3,5
print(List)
print("Swapped List: ",swapList(List,pos1-1,pos2-1))


[9, 11, 5, 3, 6, 27, 4]
Swapped List: [9, 11, 6, 3, 5, 27, 4]

Approach 3: Swapping using pop() function

In this approach we will use pop() function which is a built-in function in Python. It removes and returns the value at the given index from the list. If the index is not given then it removes and returns the last element. We will use the pop() function to remove the elements at the given position from the list and store them in separate variables.

Syntax- list.pop(index)

We will use insert() and append() which are commonly used list functions to store the elements in the swapped order in the list again.

Look at the algorithm to understand the program

Algorithm

Step 1- Define a function to swap elements with the list and positions pos1 and pos2 as parameters

Step 2- Store the first element in a variable first by using pop(pos1)

Step 3- Store the second element in a variable second by using pop(pos2-1)

Step 4- Currently the list will have n-2 elements

Step 5- Insert the two popped elements at their position

Step 6- insert(pos1,second) will store the second element at the position of the first element

Step 7- insert(pos2,first) will insert the first element at the position of the second element

Step 8- Return the swapped list

Step 9- Declare a list and pass the list and the positions in the function

Step 10- Print the list returned by the function

Program

Look at the complete program given below to understand the implementation of the approach.

#swap two element in list  
# Swap function
def swapPositions(list, pos1, pos2):
     
    # popping both the elements from list
    first = list.pop(pos1)  
    second = list.pop(pos2-1)
    
    # inserting in each others positions
    list.insert(pos1, second) 
    list.insert(pos2, first) 
     
    return list
 
# Driver function
List = [2, 6, 12, 23, 8, 9]
pos1, pos2  = 1, 3
print(List)
print("Swapped list: ",swapPositions(List, pos1-1, pos2-1))


[2, 6, 12, 23, 8, 9]
Swapped list: [12, 6, 2, 23, 8, 9]

Conclusion

In this tutorial, we have learned three ways for swapping two elements when the list and the position of the elements were given. We have discussed how to swap using the third variable, by directly swapping the elements at the given indexes and by using the pop() function.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.