Given a list, write a Python program to swap first and last element of the list.
Examples:
Input : [12, 35, 9, 56, 24] Output : [24, 35, 9, 56, 12] Input : [1, 2, 3] Output : [3, 2, 1]
Approach #1: Find the length of the list and simply swap the first element with (n-1)th element.
# Python3 program to swap first # and last element of a list # Swap function def swapList(newList): size = len(newList) # Swapping temp = newList[0] newList[0] = newList[size - 1] newList[size - 1] = temp return newList # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList)) |
Output:
[24, 35, 9, 56, 12]
Approach #2: The last element of the list can be referred as list[-1]. Therefore, we can simply swap list[0] with list[-1].
# Python3 program to swap first # and last element of a list # Swap function def swapList(newList): newList[0], newList[-1] = newList[-1], newList[0] return newList # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList)) |
Output:
[24, 35, 9, 56, 12]
Approach #3: Swap the first and last element is using tuple variable. Store the first and last element as a pair in a tuple variable, say get, and unpack those elements with first and last element in that list. Now, the First and last values in that list are swapped.
# Python3 program to swap first # and last element of a list # Swap function def swapList(list): # Storing the first and last element # as a pair in a tuple variable get get = list[-1], list[0] # unpacking those elements list[0], list[-1] = get return list # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList)) |
Output:
[24, 35, 9, 56, 12]
Approach #4: Using * operand.
This operand proposes a change to iterable unpacking syntax, allowing to specify a “catch-all” name which will be assigned a list of all items not assigned to a “regular” name.
# Python3 program to illustrate # the usage of * operarnd list = [1, 2, 3, 4] a, *b, c = list print(a) print(b) print(c) |
Output:
1 [2, 3] 4
Now let’s see the implementation of above approach:
# Python3 program to swap first # and last element of a list # Swap function def swapList(list): start, *middle, end = list list = [end, *middle, start] return list # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList)) |
Output:
[24, 35, 9, 56, 12]
Approach #5: Swap the first and last elements is to use inbuilt function list.pop(). Pop the first element and store it in a variable. Similarily pop the last element and store it in another variable. Now insert the two popped element at each other’s original position.
# Python3 program to swap first # and last element of a list # Swap function def swapList(list): first = list.pop(0) last = list.pop(-1) list.insert(0, last) list.append(first) return list # Driver code newList = [12, 35, 9, 56, 24] print(swapList(newList)) |
Output:
[24, 35, 9, 56, 12]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Get first and last elements of a list
- Python | Merge first and last elements separately in a list
- How to get the first and last elements of Deque in Python?
- Python program to capitalize the first and last character of each word in a string
- Python | Shift last element to first position in list
- Python | Replace elements in second list with index of same element in first list
- Python | Subtract two list elements if element in first list is greater
- Python - Group first elements by second elements in Tuple list
- Python | Remove last K elements of list
- Python | Get last N elements from given list
- Select first or last N rows in a Dataframe using head() and tail() method in Python-Pandas
- Python - Create a string made of the first and last two characters from a given string
- Pandas - Convert the first and last character of each word to upper case in a series
- Sort the values of first list using second list in Python
- Python | Remove last character in list of strings
- Python | How to get the last element of list
- Python | Last occurrence of some element in a list
- Python | Slicing list from Kth element to last element
- Python - Get sum of last K list items using slice
- Python program to replace first 'K' elements by 'N'
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

