Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
All pair combinations of 2 tuples in Python
Python tuples are immutable sequences used to store collections of items. When working with two tuples, you may sometimes need to generate all possible pairs, where each pair contains one element from the first tuple and one from the second. In this article, we will learn different ways to generate all pair combinations from two tuples.
Following are input-output scenarios for pairing combinations of two tuples:
<strong>Input:</strong> first_tuple = (1, 3) second_tuple = (7, 9) <strong>Output:</strong> [(1, 7), (1, 9), (3, 7), (3, 9), (7, 1), (7, 3), (9, 1), (9, 3)] <strong>Explanation:</strong> Index 0 of first_tuple pairs with index 0 of second_tuple, i.e., (1, 7). Then, index 0 of first_tuple pairs with index 1 of second_tuple, i.e., (1, 9). Next, index 1 of first_tuple pairs with index 0 of second_tuple, i.e., (3, 7). And so on...
Using itertools Module
The Python itertools module provides the product() function to combine tuples. The itertools.product() represents the Cartesian product, which pairs each element from first_tuple with every element of second_tuple ?
import itertools first_tuple = (5, 2) second_tuple = (5, 6) # Product of first_tuple * second_tuple f_tup1 = list(itertools.product(first_tuple, second_tuple)) # Product of second_tuple * first_tuple f_tup2 = list(itertools.product(second_tuple, first_tuple)) # Combine both result = f_tup1 + f_tup2 print(result)
[(5, 5), (5, 6), (2, 5), (2, 6), (5, 5), (5, 2), (6, 5), (6, 2)]
Using List Comprehension
List comprehension can generate all possible pair combinations from two tuples by iterating through each element systematically ?
first_tuple = (2, 3)
second_tuple = (6, 7)
result = [(i, j) for i in first_tuple for j in second_tuple] + \
[(i, j) for i in second_tuple for j in first_tuple]
print(result)
[(2, 6), (2, 7), (3, 6), (3, 7), (6, 2), (6, 3), (7, 2), (7, 3)]
Note: The + operator combines both combinations, while \ is the line continuation character.
Using Nested Loop
Python nested loops provide explicit control over the iteration process. Use an outer loop for the first tuple and an inner loop for the second tuple ?
first_tuple = (10, 20)
second_tuple = (50, 60)
result = []
# Nested loop iteration on first_tuple pairs to second_tuple
for a in first_tuple:
for b in second_tuple:
result.append((a, b))
# Nested loop iteration on second_tuple pairs to first_tuple
for a in second_tuple:
for b in first_tuple:
result.append((a, b))
print(result)
[(10, 50), (10, 60), (20, 50), (20, 60), (50, 10), (50, 20), (60, 10), (60, 20)]
Using map() and lambda Function
The Python map() function applies operations to iterables. We can combine map() with lambda functions and list comprehension to generate tuple pairs ?
first_tuple = (1, 7)
second_tuple = (8, 4)
# first_tuple * second_tuple
tup1 = list(map(lambda pair: (pair[0], pair[1]),
[(a, b) for a in first_tuple for b in second_tuple]))
# second_tuple * first_tuple
tup2 = list(map(lambda pair: (pair[0], pair[1]),
[(a, b) for a in second_tuple for b in first_tuple]))
# Combine both
result = tup1 + tup2
print(result)
[(1, 8), (1, 4), (7, 8), (7, 4), (8, 1), (8, 7), (4, 1), (4, 7)]
Comparison
| Method | Readability | Performance | Best For |
|---|---|---|---|
itertools.product() |
High | Fast | Large datasets |
| List Comprehension | High | Good | Pythonic code |
| Nested Loops | Medium | Good | Learning/debugging |
| map() + lambda | Low | Good | Functional programming |
Conclusion
Use itertools.product() for the most efficient and readable solution. List comprehension offers a Pythonic alternative, while nested loops provide explicit control for complex scenarios.
