Q:
Python program to interchange first and last element in a list
belongs to collection: Python List Programs
Python List Programs
- Python | Program to declare and print a list
- Python program to print list elements in different ways
- Python | Program for Adding, removing elements in the list
- Python | Program to print a list using ‘FOR and IN’ loop
- Python | Program to add an element at specified index in a list
- Python | Program to remove first occurrence of a given element in the list
- Python | Remove all occurrences a given element from the list
- Python | Program to remove all elements in a range from the List
- Python | Program to sort the elements of given list in Ascending and Descending Order
- Python | Program to find the differences of two lists
- Python | Program to Print the index of first matched element of a list
- Python | Program to find the position of minimum and maximum elements of a list
- Python | Program to input, append and print the list elements
- Python | Program to remove duplicate elements from the list
- Python | Program to Create two lists with EVEN numbers and ODD numbers from a list
- Python | Program to print all numbers which are divisible by M and N in the List
- Python | Create a list from the specified start to end index of another list
- Python | Create three lists of numbers, their squares and cubes
- Python program to find N largest and smallest elements from the list
- Python | Create two lists with first half and second half elements of a list
- Python | Iterate a list in reverse order
- Python | print list after removing EVEN numbers
- Python | print list after removing ODD numbers
- Python | Input comma separated elements, convert into list and print
- Python | Convert a string to integers list
- Using List as Stack in Python
- Python | Extend a list using + Operator
- Python program for various list operations
- Find the index of an item given a list containing it in Python
- Extract Even and odd number from a given list in Python
- Remove falsy values from a list in Python
- Python program to remove multiple elements from a list using list comprehension
- Check all elements of a list are the same or not in Python
- Check all elements are unique or not in Python
- Python program to print positive or negative numbers in a list
- Python program to extract keywords from the list
- Python program to remove empty list from a list of lists
- Python program to multiply all numbers of a list
- How to find the length of a list in Python (3 effective ways)?
- Python program to check if an element is present in list
- Python program to swap any two elements in the list
- Python program to interchange first and last element in a list
- Python program to find the cumulative sum of elements of a list
- How to clone or copy a list in Python? Cloning or copying a list
- Python program to find the sum of number digits in list
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:
Program to interchange first and last element in the list
Output:
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:
Program to interchange first and last element in the list
Output:
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:
Program to interchange first and last element in a list
Output:
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:
Program to interchange first and last element in a list
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer