Swap Two Elements in a List in Python
To swap two elements in a Python list, you can use tuple unpacking, the pop()
and insert()
methods, or the swap()
approach using a temporary variable. Below are different ways to achieve this with examples.
Examples
1. Swapping Elements in List Using Tuple Unpacking
Tuple unpacking is the most Pythonic way to swap two elements in a list.
# Creating a list
numbers = [10, 20, 30, 40, 50]
# Swapping elements at index 1 and 3
numbers[1], numbers[3] = numbers[3], numbers[1]
# Printing the updated list
print("Updated List:", numbers)
Explanation:
We swap the elements at indices 1
and 3
using tuple unpacking. The expression numbers[1], numbers[3] = numbers[3], numbers[1]
assigns the values simultaneously without needing a temporary variable.
Output:
Updated List: [10, 40, 30, 20, 50]
2. Swapping Elements in List Using a Temporary Variable
We can also swap two elements using a temporary variable.
# Creating a list
fruits = ["apple", "banana", "cherry", "date"]
# Storing one element in a temporary variable
temp = fruits[0]
fruits[0] = fruits[2]
fruits[2] = temp
# Printing the updated list
print("Updated List:", fruits)
Explanation:
We store fruits[0]
(which is “apple”) in the variable temp
. Then, we assign fruits[2]
(“cherry”) to fruits[0]
and replace fruits[2]
with temp
, effectively swapping them.
Output:
Updated List: ['cherry', 'banana', 'apple', 'date']
3. Swapping Elements in List Using the pop()
and insert()
Methods
We can use pop()
to remove elements and insert()
to place them in swapped positions.
# Creating a list
colors = ["red", "blue", "green", "yellow"]
# Popping elements from the list
color1 = colors.pop(1) # Removes 'blue'
color2 = colors.pop(2-1) # Removes 'yellow' (adjusted index after first pop)
# Inserting swapped elements
colors.insert(1, color2)
colors.insert(3, color1)
# Printing the updated list
print("Updated List:", colors)
Explanation:
We use pop()
to remove elements at indices 1 and 2 (accounting for index shift). We then insert them back in swapped positions using insert()
.
Output:
Updated List: ['red', 'yellow', 'green', 'blue']
4. Swapping Elements in List Using a Custom Function
We can create a reusable function to swap elements dynamically.
# Function to swap elements
def swap_elements(lst, idx1, idx2):
lst[idx1], lst[idx2] = lst[idx2], lst[idx1]
# Creating a list
animals = ["dog", "cat", "rabbit", "elephant"]
# Swapping elements using the function
swap_elements(animals, 0, 2)
# Printing the updated list
print("Updated List:", animals)
Explanation:
We define the function swap_elements()
that takes a list and two indices, then swaps them using tuple unpacking. This makes swapping reusable and more readable.
Output:
Updated List: ['rabbit', 'cat', 'dog', 'elephant']
Conclusion
Swapping elements in a Python list can be done using different techniques:
- Tuple unpacking: The most efficient way using
numbers[idx1], numbers[idx2] = numbers[idx2], numbers[idx1]
. - Temporary variable: Stores one value before swapping.
pop()
andinsert()
: Useful for complex swaps.- Custom function: Makes the swapping reusable.