Start learning free

LESSON 116 OF 673 · LISTS - CHALLENGES

Swap First and Last in Python: Swapping List Items

Practice Read the full explanation

Given a list of integers nums, swap its first and last numbers and return it.

Change nums itself, then return it. The numbers in between stay where they are. When nums has only one number, that number is both the first and the last, so it stays as it is.

 

Example 1:

Input: nums = [1, 2, 3, 4, 5]
Output: [5, 2, 3, 4, 1]
Explanation: 1 and 5 trade places. 2, 3 and 4 don't move.

Example 2:

Input: nums = [4, 9]
Output: [9, 4]
Explanation: With two numbers, the first and the last are next to each other, and they trade places.

Example 3:

Input: nums = [7]
Output: [7]
Explanation: One number is both the first and the last, so nothing changes.

 

Constraints:

  • nums has at least one number.
  • Every number is an integer between -1000 and 1000.
STEP 1 OF 4
    Ctrl + Enter to check

    PYTHON · LISTS - CHALLENGES

    Swap First And Last in Python, explained

    Problem overview

    The method swapFirstAndLast receives a list of integers, nums, with at least one number. It swaps the first and last numbers, then returns the list. The list itself is changed, and the numbers in between stay where they are. When the list has a single number, that number is both the first and the last, so the list stays as it is.

    For [1, 2, 3, 4, 5] the result is [5, 2, 3, 4, 1]. For [4, 9] it is [9, 4]. For [7] it is [7].

    Approach

    Swapping two values has a classic trap. If you copy the last number into the first place, the old first number is overwritten and lost. To avoid that, save it somewhere first. A temporary variable does the job: copy the first number into temp, overwrite the first place with the last number, then write temp into the last place. It works like swapping the drinks in two cups with a third cup.

    Lists are mutable, so assigning to nums[0] changes the list that the caller passed in. That is exactly what the task asks for: change nums, then return it.

    The same idea applies to any swap, not just the ends of a list. Whenever two places must trade values, one value needs to wait while the other is copied over it. Tracing the list after each assignment, as in the steps below, is the easiest way to see why the temporary variable is needed.

    Step by step

    1. Save the first number. temp = nums[0]. For [1, 2, 3, 4, 5], temp is 1.
    2. Copy the last number into the first place. nums[0] = nums[-1]. The index -1 means the last item whatever the length. The list is now [5, 2, 3, 4, 5], and the old first number survives only in temp.
    3. Put the saved number in the last place. nums[-1] = temp. The list is now [5, 2, 3, 4, 1].
    4. Return the list. It is the same list object, now swapped.

    The solution in Python

    class Solution:
        def swapFirstAndLast(self, nums: List[int]) -> List[int]:
            temp = nums[0]
            nums[0] = nums[-1]
            nums[-1] = temp
            return nums
    

    Some details worth knowing:

    • The function changes the caller's list and returns that same list, not a copy. After the call, the list that was passed in is swapped too.
    • With a single number, nums[0] and nums[-1] are the same place. Saving it, copying it onto itself and writing it back leaves [7] as it was.
    • Python can also swap without a temporary variable: nums[0], nums[-1] = nums[-1], nums[0]. The right side is evaluated first, then both assignments happen. The lesson uses temp because it makes each step visible.

    Edge cases and common mistakes

    • Skipping the temporary variable. The two lines nums[0] = nums[-1] and nums[-1] = nums[0] in a row copy the last number into both places. For [1, 2, 3] the result is [3, 2, 3], because the first number was lost.
    • Two numbers. [4, 9] works with no special case: temp is 4, the list becomes [9, 9], then [9, 4].
    • An empty list. The lesson never passes one, but [][0] raises IndexError: list index out of range.
    • Returning a new list instead. The task says to change nums itself, so build the swap with assignments to nums, not with a copy.

    Testing

    • [1, 2, 3, 4, 5] gives [5, 2, 3, 4, 1].
    • [4, 9] gives [9, 4].
    • [7] gives [7].
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free