In this article, we will show how to write a Python program to interchange the first and last elements in a List with examples. There are multiple options to swap the first and last item in a list, including using a temp variable, assignment operator, loops, pop function, etc.
Python Program to Interchange First and Last Elements in a List Using temp variable
In the program below, we have declared an integer list of five items. Our task is to replace, swap, or interchange the first and the last elements from the list (10 and 999 Positions).
Within the program, first, we use the len() function for finding the total list items. Next, assign the first value to the temporary variable and assign the last temp as the first element. Next, assign the value inside the temp variable to the last element.
a = [10, 50, 100, 200, 999]
print("Before: ", a)
size = len(a)
#swap
temp = a[0]
a[0] = a[size - 1]
a[size - 1] = temp
print("After Swap:", a)
Before: [10, 50, 100, 200, 999]
After Swap: [999, 50, 100, 200, 10]
As we all know, the list items can also be accessed using the negative index, where negative 1 (-1) is the last index position. So, instead of using len() to get the last item, in the program, we have used the -1 to get the last element.
Here,
temp = a[-1] = 555
a[-1] = a[0] = 111
a[0] = temp = 555
So, the final list will be a = [555, 50, 75, 125, 375, 111].
a = [111, 50, 75, 125, 375, 555]
print("Before: ", a)
#swap
temp = a[-1]
a[-1] = a[0]
a[0] = temp
print("After Swap:", a)
Using Assignment Operators
In Python, we can use the assignment operator to assign multiple values to the variables in one statement. Here, in this Python program, we used the shorthand approach of the assignment operator for interchanging the first and last elements of a list.
a = [10, 50, 100, 150, 200, 999]
print("Before: ", a)
a[0], a[-1] = a[-1], a[0]
print("After Swap:", a)
Before: [10, 50, 100, 150, 200, 999]
After Swap: [999, 50, 100, 150, 200, 10]
Python Program to Interchange First and Last Elements in a List using pop, insert, and append functions
In this program, we defined a function that accepts the list as the parameter value. Within the function, the pop() function will remove an item at the given index position.
Here, a.pop(0) removes the starting item, and we assign it to the first variable. Similarly, we assigned the end value to the last variables. As the pop() function removes the first and last items, the remaining list will be [50, 100, 150, 200].
In the next line, the insert function will add the last value at the index position 0, which is the first position. Next, the append function will add the list item at the end or last position.
def swapListItems(a):
first = a.pop(0)
last = a.pop(-1)
a.insert(0, last)
a.append(first)
return a
num = [10, 50, 100, 150, 200, 999]
print("Before: ", num)
print("After Swap:", swapListItems(num))
Before: [10, 50, 100, 150, 200, 999]
After Swap: [999, 50, 100, 150, 200, 10]
Using list slicing technique
This Python program uses the list slicing technique to interchange the first and last elements in an integer List. To understand better, we have printed each slice of the string concatenation in a separate print statement.
First, it checks whether the list has at least two values. Next, we use the slicing technique to extract the first and last items. Next, a[1:-1] will extract the last item from the second position to the last but one position. Next, add those three slices to get the final result.
a = [25, 50, 100, 150, 200, 400]
print("Before: ", a)
if len(a) >= 2:
a = a[-1:] + a[1:-1] + a[:1]
print("After Swap:", a)
print("a[-1:] = ", a[-1:])
print("a[1: -1] = ", a[1: -1])
print("a[:1] = ", a[:1])
Before: [25, 50, 100, 150, 200, 400]
After Swap: [400, 50, 100, 150, 200, 25]
a[-1:] = [25]
a[1: -1] = [50, 100, 150, 200]
a[:1] = [400]
Python Program to Interchange First and Last Elements in a List Using * operand
Here, the * operand on list items unpacks iterables. So, (a, *b, c = num) means the first list item will be assigned to a and the last element to c. The remaining list items will be assigned to *b. Next, we reassigned them by interchanging the first and last position items. For your understanding, we have added three print statements inside a swapListItems function.
def swapListItems(num):
a, *b, c = num
print(a)
print(*b)
print(c)
num = [c, *b, a]
return num
numList = [25, 50, 100, 150, 200, 400]
print("Before: ", numList)
print("After:", swapListItems(numList))
Before: [25, 50, 100, 150, 200, 400]
25
50 100 150 200
400
After: [400, 50, 100, 150, 200, 25]