We will simply need to swap elements at the first index with the element at the last index in the list.
Python provides multiple ways to perform the task. Let's see them,
Method 1:
In this method, we will find the length of the list. And then swap the element at index 0 and element at index (length - 1).
Algorithm:
Find the length of the list
Swap values at list[0] and list[len - 1]
temp = list[0]
list[0] = list[length - 1]
list[length - 1] = temp
Return the list
Program to interchange first and last element in the list
# Python program to interchange the
# first and last element of a list
# Creating a list
myList = [1, 7, 3, 90, 23, 4]
print("Initial List : ", myList)
# finding the length of list
length = len(myList)
# Swapping first and last element
temp = myList[0]
myList[0] = myList[length - 1]
myList[length - 1] = temp
print("List after Swapping : ", myList)
Output:
Initial List : [1, 7, 3, 90, 23, 4]
List after Swapping : [4, 7, 3, 90, 23, 1]
Method 2:
Another method to interchange values is using Python's shorthand technique to find the last element. And then swapping them.
The last element can be found using list[-1].
Algorithm:
swap -> list[0] and list[-1].
temp = list[-1]
list[-1] = list[-0]
list[0] = temp
Print list
Program to interchange first and last element in the list
# Python program to interchange the
# first and last element of a list
# Creating a list
myList = [1, 7, 3, 90, 23, 4]
print("Initial List : ", myList)
# Swapping first and last element
temp = myList[-1]
myList[-1] = myList[0]
myList[0] = temp
print("List after Swapping : ", myList)
Output:
Initial List : [1, 7, 3, 90, 23, 4]
List after Swapping : [4, 7, 3, 90, 23, 1]
Method 3:
In this method, we will use the comma assignment method from python. Here, we will store the first and last element tuple to the last and first element tuple.
Syntax:
list[0], list[-1] = list[-1], list[0]
Program to interchange first and last element in a list
# Python program to interchange the
# first and last element of a list
# Creating a list
myList = [1, 7, 3, 90, 23, 4]
print("Initial List : ", myList)
# Swapping first and last element
myList[0], myList[-1] = myList[-1], myList[0]
print("List after Swapping : ", myList)
Output:
Initial List : [1, 7, 3, 90, 23, 4]
List after Swapping : [4, 7, 3, 90, 23, 1]
Method 4:
One more method, can be deleting the elements from the list and then inserting them back in the desired position.
Here, we will delete (pop) the first and the last elements of the list and insert them back as the first element at the last position and last element at the first position.
Algorithm:
Swap values
firstVal = list.pop(0)
lastVal = list.pop(-1)
list.insert(0, lastVal)
list.append(firstVal)
Print list
Program to interchange first and last element in a list
# Python program to interchange the
# first and last element of a list
# Creating a list
myList = [1, 7, 3, 90, 23, 4]
print("Initial List : ", myList)
# Swapping first and last element
firstVal = myList.pop(0)
lastVal = myList.pop(-1)
myList.insert(0, lastVal)
myList.append(firstVal)
print("List after Swapping : ", myList)
Output:
Initial List : [1, 7, 3, 90, 23, 4]
List after Swapping : [4, 7, 3, 90, 23, 1]
We will simply need to swap elements at the first index with the element at the last index in the list.
Python provides multiple ways to perform the task. Let's see them,
Method 1:
In this method, we will find the length of the list. And then swap the element at index 0 and element at index (length - 1).
Algorithm:
Program to interchange first and last element in the list
Output:
Method 2:
Another method to interchange values is using Python's shorthand technique to find the last element. And then swapping them.
The last element can be found using list[-1].
Algorithm:
Program to interchange first and last element in the list
Output:
Method 3:
In this method, we will use the comma assignment method from python. Here, we will store the first and last element tuple to the last and first element tuple.
Syntax:
Program to interchange first and last element in a list
Output:
Method 4:
One more method, can be deleting the elements from the list and then inserting them back in the desired position.
Here, we will delete (pop) the first and the last elements of the list and insert them back as the first element at the last position and last element at the first position.
Algorithm:
Program to interchange first and last element in a list
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer