Python program to interchange first and last element in a list

Interchange the first and last element of the list: In this tutorial, we will learn how to interchange the first and last elements of the given list in Python using multiple approaches. Submitted by Shivang Yadav, on April 06, 2021

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 [].

Problem statement

Given a Python list, you have to write a Python program to interchange first and last elements of the list.

Example

Consider the below example with sample input and output:

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,

1. Interchange first and last elements in a list using length and swapping

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

Example

# 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)

The output of the above example is:

Initial List :  [1, 7, 3, 90, 23, 4]
List after Swapping :  [4, 7, 3, 90, 23, 1]

2. Interchange first and last elements in a list using shorthand technique

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

Example

# 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)

The output of the above example is:

Initial List :  [1, 7, 3, 90, 23, 4]
List after Swapping :  [4, 7, 3, 90, 23, 1]

3. Interchange first and last elements in a list using comma assignment method

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]

Example

# 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)

The output of the above example is:

Initial List :  [1, 7, 3, 90, 23, 4]
List after Swapping :  [4, 7, 3, 90, 23, 1]

4. Interchange first and last elements in a list using deleting and inserting elements

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

Example

# 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)

The output of the above example is:

Initial List :  [1, 7, 3, 90, 23, 4]
List after Swapping :  [4, 7, 3, 90, 23, 1]

Python List Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.