Python Program to Interchange First and Last Elements in list
In this tutorial, you will learn how to interchange the first and the last elements in a list in Python. Lists in Python are a group of ordered values that are closed within a square[] bracket. For a given list, we have to write a Python program that will swap the first and the last elements of a list. Swapping means exchanging the values of the two numbers. Suppose you have two variables a and b. The value of a is 40 and the value of b is 30. After swapping the value of a will become 30 and the value of b will become 40.
For a list a[ ]= [1, 2, 3, 4, 5], swap elements at position 1 and position 5
To get the final list as a[]=[5,2,3,4,1]
Example
Look at the examples to understand the input and output Values:
Input : [14, 3, 56, 17, 8, 10]
Output : [10, 3, 56, 17, 8, 14]
Approach To Solve the Problem
To execute this task, we can follow multiple approaches, some are discussed below:
- By using a third variable for swapping the values present at index 0 and index n-1 where n is the size of the list.
- By using a built-in pop() function to pop the first element and store it in a variable.
- By using the concept of negative indexing to access the last element in the list.
We will discuss all three approaches in detail separately.
Approach 1: Interchange List Elements
In this approach, we will use a third variable to swap the values present at the first and the last index of the list.
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 as a parameter.
Step 2- Swap elements sl[0] and sl[n-1] using a third variable.
Step 3- Return the swapped list.
Step 4- Define the list values.
Step 5- Pass the list in the function 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 first and last element in list
# Swap function
def swapList(sl):
n = len(sl)
# Swapping
temp = sl[0]
sl[0] = sl[n - 1]
sl[n - 1] = temp
return sl
l = [10, 14, 5, 9, 56, 12]
print(l)
print("Swapped list: ",swapList(l))
[10, 14, 5, 9, 56, 12]
Swapped list: [12, 14, 5, 9, 56, 10]
Approach 2: 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 first and last elements from the list and store them in separate variables.
Look at the algorithm to understand the program
Algorithm
Step 1- Define a function to swap elements
Step 2- Store the first element in a variable first by using pop(0)
Step 3- Store the last element in a variable last by using pop(-1)
Step 4- Currently the list will have n-2 elements
Step 5- Insert the two popped elements at their position
Step 6- Insert(0, last) will store the last element at the starting position
Step 7- Use append(first) to insert the first element at the last position
Step 8- Return the swapped list
Step 9- Declare a list and pass the list in the function
Step 10- Print the list returned by the function
Python Program2
Look at the complete program given below to understand the implementation of the approach.
#swap first and last element in list
# Swap function
def swapList(list):
first = list.pop(0)
last = list.pop(-1)
list.insert(0, last)
list.append(first)
return list
li = [1, 9, 2, 10, 19, 30]
print(li)
print("Swapped list: ",swapList(li))
[1, 9, 2, 10, 19, 30]
Swapped list: [30, 9, 2, 10, 19, 1]
Approach 3: Negative Indexing
In this approach, we will use the concept of negative indexing. As we know, that the elements of the list can be accessed through their index. In Python, elements can also have negative indexes.
a[]= [1, 10, 16, 27, 19, 20, 13]
Index- 0 1 2 3 4 5 6
-7 -6 -5 -4 -3 -2 -1
Hence, to access the last element in a list we can use -1 as the 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 0 and -1
Step 3- Return the swapped list
Step 4- Declare list values
Step 5- Pass the list in the function and display the result
Python Program 3
Look at the complete program given below.
#swap first and last element in list
# Swap function
def swapList(sl):
sl[0], sl[-1] = sl[-1], sl[0]
return sl
List = [9, 11, 5, 3, 6, 27, 4]
print(List)
print("Swapped List: ",swapList(List))
[9, 11, 5, 3, 6, 27, 4]
Swapped List: [4, 11, 5, 3, 6, 27, 9]
Conclusion
In this tutorial, we have learned, three ways of interchanging the values at the first and last position of a list. We have discussed the concept of swapping using a third variable and direct swapping method. We have also mentioned built-in functions in list like pop() insert() and append().