Python program to interchange first and last element in a list

Here, we will write a Python program in which we will interchange the first and list element of the list.
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 [].

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.

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]

Python List Programs »





Comments and Discussions




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.