Python program to interchange first and last elements in a list
Given a list, write a Python program to swap the first and last element of the list using Python.
Examples: The last element of the list can be referred to as a list[-1]. Therefore, we can simply swap list[0] with list[-1].
# Initialize a list
my_list = [1, 2, 3, 4, 5]
# Interchange first and last elements
my_list[0], my_list[-1] = my_list[-1], my_list[0]
# Print the modified list
print("List after swapping first and last elements:", my_list)
Output
List after swapping first and last elements: [5, 2, 3, 4, 1]
Interchange first and last elements using Temporary Value
Find the length of the list and simply swap the first element with (n-1)th element.
# 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]
Swapping first and last items in a list using tuple variable
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.
# 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]
Swap first and last values in a list in Python 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.
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:
# 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]
Swap first and last values in a list in Python using Slicing
In this approach, we first check if the list has at least 2 elements. If the list has at least 2 elements, we swap the first and last elements using Python slicing by assigning the value of the last element to the first element and the value of the first element to the last element. We then slice the list from the second element to the second-to-last element and concatenate it with a list containing the first element and the last element in their new positions.
def swap_first_last_3(lst):
# Check if list has at least 2 elements
if len(lst) >= 2:
# Swap the first and last elements using slicing
lst = lst[-1:] + lst[1:-1] + lst[:1]
return lst
# Initializing the input
inp=[12, 35, 9, 56, 24]
# Printing the original input
print("The original input is:",inp)
result=swap_first_last_3(inp)
# Printing the result
print("The output after swap first and last is:",result)
Output
The original input is: [12, 35, 9, 56, 24] The output after swap first and last is: [24, 35, 9, 56, 12]
Time Complexity: O(1)
Space Complexity: O(1)
FAQs
How do you find the first and last items in a list Python?
To find the first and last items in a list in Python, you can use indexing. The first item in a list has index 0, and the last item has index -1. Here’s how you can do it:
# Sample list
my_list = [1, 2, 3, 4, 5]
# Find the first item
first_item = my_list[0]
# Find the last item
last_item = my_list[-1]
How to switch the first and last characters in a string Python?
To switch the first and last characters in a string in Python, you can use string slicing and concatenation. Here’s how you can do it:
# Sample string
my_string = “”hello””
# Switch first and last characters
new_string = my_string[-1] + my_string[1:-1] + my_string[0]
How to shift elements in a list in Python?
To shift elements in a list in Python, you can use slicing and concatenation. Here’s how you can shift elements to the left by one position:
# Sample list
my_list = [1, 2, 3, 4, 5]
# Shift elements to the left by one position
shifted_list = my_list[1:] + [my_list[0]]
How to move an element to the end of a list in Python?
To move an element to the end of a list in Python, you can use list methods such as pop() and append(). Here’s how you can move the first element to the end of the list:
# Sample list
my_list = [1, 2, 3, 4, 5]# Move the first element to the end
my_list.append(my_list.pop(0))
How to swap characters in a list?
To swap characters in a string in Python, you can convert the string to a list, perform the swap, and then convert the list back to a string. Here’s how you can swap the first and last characters:
# Sample string
my_string = “hello”# Convert string to list
my_list = list(my_string)# Swap first and last characters
my_list[0], my_list[-1] = my_list[-1], my_list[0]# Convert list back to string
new_string = “”.join(my_list)