Swap tuple elements in list of tuples - Python
Last Updated :
11 Feb, 2025
The task of swapping tuple elements in a list of tuples in Python involves exchanging the positions of elements within each tuple while maintaining the list structure. Given a list of tuples, the goal is to swap the first and second elements in every tuple. For example, with a = [(3, 4), (6, 5), (7, 8)], the result would be [(4, 3), (5, 6), (8, 7)]
Using list comprehension
List comprehension is the most efficient and readable way to swap tuple elements in a list. It iterates through the list and swaps each tuple’s elements in a single step, making the code concise and fast.
Python
a = [(3, 4), (6, 5), (7, 8)]
res = [(x, y) for y, x in a] # Swaps elements using tuple unpacking
print(res)
Output[(4, 3), (5, 6), (8, 7)]
Explanation: For each tuple (x, y), it swaps the elements using tuple unpacking (y, x → x, y). The modified tuples are stored in res .
Using map()
map() applies a swapping operation to each tuple using a lambda function. This method is useful when working with large datasets, as it processes elements efficiently without creating an intermediate list during iteration.
Python
a = [(3, 4), (6, 5), (7, 8)]
res = list(map(lambda sub: (sub[1], sub[0]), a))
print(res)
Output[(4, 3), (5, 6), (8, 7)]
Explanation: map() apply a lambda function to each tuple in the list a. It swaps the elements by accessing them as sub[1], sub[0]. The map() result is converted to a list and stored in res .
Using zip()
This method swaps tuple elements by first unpacking them using zip(*a), then reconstructing the list. While not the most efficient, it can be helpful when dealing with structured transformations in tuple-based data.
Python
a = [(3, 4), (6, 5), (7, 8)]
res = list(zip(*[(y, x) for x, y in a]))
res = list(zip(res[0], res[1])) # Convert back to list of tuples
print(res)
Output[(4, 3), (5, 6), (8, 7)]
Explanation: This code swaps tuple elements using list comprehension, then transposes them with zip(*...). The second zip() reconstructs the list of swapped tuples, which is then printed.
Using for loop
for loop manually iterates through the list, swaps elements, and appends the modified tuples to a new list. While easy to understand, it is slower than other methods due to explicit list operations and function calls.
Python
a = [(3, 4), (6, 5), (7, 8)]
res = []
for x, y in a:
res.append((y, x)) # Manually swapping elements
print(res)
Output[(4, 3), (5, 6), (8, 7)]
Explanation: for loop iterates through each tuple in a, manually swaps the elements (y, x) and appends the swapped tuple to res .
Similar Reads
Python - Sort Tuple List by Nth Element of Tuple We are given list of tuple we need to sort tuple by Nth element of each tuple. For example d = [(1, 5), (3, 2), (2, 8), (4, 1)] and k=1 we need to sort by 1st element of each tuple so that output for given list should be [(4, 1), (3, 2), (1, 5), (2, 8)]Using sorted() with lambdasorted() function wit
3 min read
Flatten tuple of List to tuple - Python The task of flattening a tuple of lists to a tuple in Python involves extracting and combining elements from multiple lists within a tuple into a single flattened tuple. For example, given tup = ([5, 6], [6, 7, 8, 9], [3]), the goal is to flatten it into (5, 6, 6, 7, 8, 9, 3). Using itertools.chain(
3 min read
Python | Reverse each tuple in a list of tuples Given a list of tuples, write a Python program to reverse each tuple in the given list of tuples. Examples: Input : [(1, 2), (3, 4, 5), (6, 7, 8, 9)] Output : [(2, 1), (5, 4, 3), (9, 8, 7, 6)] Input : [('a', 'b'), ('x', 'y'), ('m', 'n')] Output : [('b', 'a'), ('y', 'x'), ('n', 'm')] Method #1 : Nega
5 min read
Python Program to Swap Two Elements in a List In this article, we will explore various methods to swap two elements in a list in Python. The simplest way to do is by using multiple assignment.Example:Pythona = [10, 20, 30, 40, 50] # Swapping elements at index 0 and 4 # using multiple assignment a[0], a[4] = a[4], a[0] print(a)Output[50, 20, 30,
1 min read
Update Each Element in Tuple List - Python The task of updating each element in a tuple list in Python involves adding a specific value to every element within each tuple in a list of tuples. This is commonly needed when adjusting numerical data stored in a structured format like tuples inside lists. For example, given a = [(1, 3, 4), (2, 4,
3 min read