Python program to interchange first and last elements in a list

Created with Sketch.

Python program that interchanges the first and last elements of a list:

def interchange_first_last(lst):
    # Check if the list has at least two elements
    if len(lst) < 2:
        return lst

    # Swap the first and last elements
    lst[0], lst[-1] = lst[-1], lst[0]

    return lst

# Example usage:
my_list = [1, 2, 3, 4, 5]
print(interchange_first_last(my_list))  # Output: [5, 2, 3, 4, 1]

In this program, the interchange_first_last() the function takes a list as its argument and returns the list with its first and last elements interchanged. The function first checks if the list has at least two elements. If it does, the function swaps the first and last elements using Python’s tuple assignment feature (lst[0], lst[-1] = lst[-1], lst[0]). Finally, the function returns the modified list.

In the example usage, the program creates a list my_list containing the integers 1 through 5. The program then calls the interchange_first_last() function with my_list as its argument and prints the result. The output shows that the function successfully interchanges the first and last elements of the list.

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.

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 to 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 using a tuple variable. Store the first and last element as a pair in a tuple variable, say get, and unpack those elements with the 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 the 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 to use the inbuilt function list.pop(). Pop the first element and store it in a variable. Similarly, pop the last element and store it in another variable. Now insert the two popped elements 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]

One Response

Leave a Reply

Your email address will not be published. Required fields are marked *