How do you interchange the first and last element of a list in Python?

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of ordered sets of values enclosed in square brackets [].

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]

Solution Approach:

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.

In this tutorial, we will learn how to interchange or swap first and last element of a list in python. 

This problem can be solved using basic techniques. This task asks you to interchange two elements of a list. Like array, each element of a list has a distinct position assigned to them starting from 0. Lists are mutable hence elements of the list can easily be replaced, new elements can be added or deleted from any position in the list. Also, lists accept all types of data types like integer, strings, etc.

Lists are written in square brackets. append() method is used to add new values in the list. Here is an example of list and appending new values to it:

In this program, we used an iterable unpacking concept in which we can get the first and last elements easily. The syntax is a little strange but very useful. We should use this as it is a built-in feature of Python. See the Python code.

Here is source code of the Python Program to swap the first and last value of a list. The program output is also shown below.

a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
    element=int(input("Enter element" + str(x+1) + ":"))
    a.append(element)
temp=a[0]
a[0]=a[n-1]
a[n-1]=temp
print("New list is:")
print(a)

Program Explanation

1. User must enter the number of elements in the list and store it in a variable.
2. User must enter the values of elements into the list.
3. The append function obtains each element from the user and adds the same to the end of the list as many times as the number of elements taken.
4. A temporary variable is used to swap the first and last element in the list.
5. The newly formed list is printed.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

advertisement

advertisement

Runtime Test Cases

 
Case 1:
Enter the number of elements in list:4
Enter element1:23
Enter element2:45
Enter element3:67
Enter element4:89
New list is:
[89, 45, 67, 23]
 
Case 2:
Enter the number of elements in list:3
Enter element1:56
Enter element2:34
Enter element3:78
New list is:
[78, 34, 56]

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Check this: Python Books | Information Technology Books

« Prev - Python Program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple

How do you interchange first and last elements in a list Python?

Python Program to Swap the First and Last Element in a List.
Take the number of elements in the list and store it in a variable..
Accept the values into the list using a for loop and insert them into the list..
Using a temporary variable, switch the first and last element in the list..
Print the newly formed list..

How do you interchange first and last element?

We will use a temporary variable to interchange the first and last elements in a list. First, we will store the first element in a temporary variable. Then, we assign the last element to the first element. Lastly, we assign a temporary variable to the last element.

How do you interchange values in a list Python?

To swap two list elements x and y by value, get the index of their first occurrences using the list. index(x) and list. index(y) methods and assign the result to variables i and j , respectively. Then apply the multiple assignment expression lst[i], lst[j] = lst[j], lst[i] to swap the elements.

How do you replace the first and last character of a string in Python?

We initialize a variable start, which stores the first character of the string (string[0]).
We initialize another variable end that stores the last character (string[-1]).
Then we will use string slicing, string[1:-1], this will access all the characters from the 2nd position excluding the last character..