HOWTO · Python

How to Swap Elements of a List in Python

This tutorial demonstrates how to swap the elements of a list in Python.

On this page

Swapping elements in a Python list changes which values occupy their positions. Python’s tuple unpacking makes a pair exchange concise. A temporary variable makes each assignment explicit, while pop() with insert() moves values through the list. Slice assignment is useful when the goal is to reverse a range rather than exchange only two positions.

Swap List Elements with Tuple Unpacking

Tuple unpacking evaluates the values on the right before assigning them to the indexed targets on the left. That lets each target receive the other position’s original value without a separate storage variable. Python indexes lists from zero, so the first element is at index 0.

For two known positions, use this direct form. Run this example:

items = ["red", "blue", "green", "gold"]
items[1], items[3] = items[3], items[1]
print(items)
['red', 'gold', 'green', 'blue']

This swaps the second and fourth values in place. The list keeps the same length, and elements outside those indexes stay in their original positions.

For several independent pairs, put the index pairs in an iterable and apply the same assignment to each pair:

items = [6, 2, 7, 8, 5, 9, 10, 3]
for left, right in [(0, 3), (4, 6)]:
    items[left], items[right] = items[right], items[left]
print(items)
[8, 2, 7, 6, 10, 9, 5, 3]

Each iteration swaps one pair using the list’s current values. Choose pairs whose indexes refer to the intended positions; if pairs overlap, an earlier swap can affect what a later swap sees.

Swap Elements with a Temporary Variable

A temporary variable shows the three assignments that make a swap work. First save one value, then overwrite its list position with the other value, and finally put the saved value in the second position. This is useful when spelling out the steps makes surrounding logic easier to follow.

items = [6, 2, 7, 8]
temp = items[1]
items[1] = items[3]
items[3] = temp
print(items)

The temporary variable must hold the original value before its list position is overwritten. For a simple pair, tuple unpacking is shorter; both approaches update the existing list.

Use pop() and insert() to Swap Elements

pop() is a list method that removes an item and returns its value. Removing an item shifts later elements one position to the left, so indexes after a removal may refer to different values. insert() is another list method; it places a value before the specified position and shifts later elements right.

To exchange the values originally at indexes 1 and 3, remove the lower index first. The value originally at index 3 then moves to index 2:

items = [6, 2, 7, 8]
first = items.pop(1)
second = items.pop(2)
items.insert(1, second)
items.insert(3, first)
print(items)

The second pop() uses index 2 because the first removal shifted the remaining items. After both removals, inserting the saved values at positions 1 and 3 restores the original list length with the pair exchanged. This approach takes more steps than direct assignment and is mainly useful when removal and reinsertion are already part of the operation.

Reverse a Slice to Swap a Range of Elements

A slice selects a range using start:stop:step. The start index is included and the stop index is excluded; a negative step visits selected values in reverse order. Slice assignment replaces the selected portion of a list with another sequence.

items = [6, 2, 7, 8]
items[1:4] = items[1:4][::-1]
print(items)

Here the selected range contains indexes 1, 2, and 3, so the assignment reverses all three values. It does not only exchange the endpoints. To exchange exactly two adjacent values by reversing a range, select a two-element range; for two arbitrary indexes, tuple unpacking is clearer.

Choose an Approach and Check Indexes

Use tuple unpacking for a direct pair swap and a loop over index pairs when applying independent swaps. Use a temporary variable when the explicit sequence helps explain the surrounding code. pop() and insert() change list positions as they remove and add values, so recalculate indexes after each removal. Use slice assignment when reversing the selected range is the intended result.

List indexes start at 0. An indexed read or assignment with an index outside the list’s valid positions raises IndexError. Before swapping, check that each index is within the list and that the two positions represent the values you mean to exchange. These checks are especially useful when indexes come from user input or are computed from other data.