Python program to interchange first and last elements in a list

In this article, we will learn how to interchange the first and last elements in a Python list. This is a common programming task with multiple implementation approaches.

Problem statement − We are given a list, and we need to swap the first element with the last element.

There are 4 approaches to solve this problem as discussed below −

Using Temporary Variable

The most straightforward approach uses a temporary variable to hold one element during the swap operation ?

def swapLast(data_list):
    size = len(data_list)
    # Swap operation using temporary variable
    temp = data_list[0]
    data_list[0] = data_list[size - 1]
    data_list[size - 1] = temp
    return data_list

# Driver code
data_list = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
print("Original list:", data_list)
result = swapLast(data_list)
print("After swapping:", result)
Original list: ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
After swapping: ['l', 'u', 't', 'o', 'r', 'i', 'a', 't']

Using Negative Indexing

This approach is similar to the first one but uses Python's negative indexing to access the last element ?

def swapLast(data_list):
    # Swap operation using negative indexing
    temp = data_list[0]
    data_list[0] = data_list[-1]
    data_list[-1] = temp
    return data_list

# Driver code
data_list = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
print("Original list:", data_list)
result = swapLast(data_list)
print("After swapping:", result)
Original list: ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
After swapping: ['l', 'u', 't', 'o', 'r', 'i', 'a', 't']

Using Tuple Packing and Unpacking

Python allows simultaneous assignment using tuple packing and unpacking, making the swap operation more concise ?

def swapLast(data_list):
    # Packing the elements into a tuple
    get = data_list[-1], data_list[0]
    # Unpacking those elements
    data_list[0], data_list[-1] = get
    return data_list

# Driver code
data_list = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
print("Original list:", data_list)
result = swapLast(data_list)
print("After swapping:", result)
Original list: ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
After swapping: ['l', 'u', 't', 'o', 'r', 'i', 'a', 't']

Using Extended Unpacking

This approach uses Python's extended unpacking syntax with the * operator to separate the first, middle, and last elements ?

def swapLast(data_list):
    # Unpacking first, middle elements, and last
    start, *middle, end = data_list
    # Reconstructing with swapped positions
    data_list = [end, *middle, start]
    return data_list

# Driver code
data_list = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
print("Original list:", data_list)
result = swapLast(data_list)
print("After swapping:", result)
Original list: ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
After swapping: ['l', 'u', 't', 'o', 'r', 'i', 'a', 't']

Comparison

Method Readability Memory Usage Best For
Temporary Variable High Low Beginners
Negative Indexing High Low Cleaner code
Tuple Packing Medium Low Pythonic style
Extended Unpacking Medium Higher Advanced users

Conclusion

All four methods effectively swap the first and last elements in a list. The tuple packing approach is most Pythonic, while the temporary variable method is clearest for beginners.

Updated on: 2026-03-25T07:03:38+05:30

827 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements