- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to interchange first and last elements in a list
In this article, we will learn about the solution to the problem statement given below.
Problem statement − We are given a list, we need to swap the last element with the first element.
There are 4 approaches to solve the problem as discussed below−
Approach 1 − The brute-force approach
Example
def swapLast(List): size = len(List) # Swap operation temp = List[0] List[0] = List[size - 1] List[size - 1] = temp return List # Driver code List = ['t','u','t','o','r','i','a','l'] print(swapLast(List))
Output
['t','u','t','o','r','i','a','l']
Approach 2 − The brute-force approach using negative indexes
Example
def swapLast(List): size = len(List) # Swap operation temp = List[0] List[0] = List[-1] List[-1] = temp return List # Driver code List = ['t','u','t','o','r','i','a','l'] print(swapLast(List))
Output
['t','u','t','o','r','i','a','l']
Approach 3 − The packing and unpacking of a tuple
Example
def swapLast(List): #packing the elements get = List[-1], List[0] # unpacking those elements List[0], List[-1] = get return List # Driver code List = ['t','u','t','o','r','i','a','l'] print(swapLast(List))
Output
['t','u','t','o','r','i','a','l']
Approach 4 − The packing and unpacking of a tuple
Example
def swapLast(List): #packing the elements start, *middle, end = List # unpacking those elements List = [end, *middle, start] return List # Driver code List = ['t','u','t','o','r','i','a','l'] print(swapLast(List))
Output
['t','u','t','o','r','i','a','l']
Conclusion
In this article, we have learned about how we can interchange first and last elements in a list
- Related Articles
- Java Program to Interchange Elements of First and Last in a Matrix Across Rows
- Java Program to Interchange Elements of First and Last in a Matrix Across Columns
- Get first and last elements of a list in Python
- Java Program to Get First and Last Elements from an Array List
- Python Program to Swap the First and Last Value of a List
- Python – Find the distance between first and last even elements in a List
- How to change first and last elements of a list using jQuery?
- Python Program to Reverse only First N Elements of a Linked List
- Java Program to Add Element at First and Last Position of a Linked list
- C program to interchange the diagonal elements in given matrix
- Program to find a sublist where first and last values are same in Python
- Get last N elements from given list in Python
- C# Program to get the first three elements from a list
- Get first and last elements from Vector in Java
- How to get first and last elements from ArrayList in Java?

Advertisements