Q:

Python program to interchange first and last element in a list

belongs to collection: Python List Programs

0

Python program to interchange first and last element in last

In this program, we will swap the first and last elements of the list.

Example:

Input:
[4, 1, 7, 3, 90, 23, 56]

Output:
[56, 1, 7, 3, 90, 23, 4]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python List Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to find the cumulative sum of eleme... >>
<< Python program to swap any two elements in the lis...