
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
All pair combinations of 2 tuples in Python
Python tuples are immutable sequences that 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:
Input: first_tuple = (1, 3) second_tuple = (7, 9) Output: [(1, 7), (1, 9), (3, 7), (3, 9), (7, 1), (7, 3), (9, 1), (9, 3)] Explanation: At 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...
Generating All Pairs Combination of 2 Tuples in Python
Following is the list of approaches to perform the task all pair combinations of 2-tuples using Python:
Using itertools Module
The Python itertools module is used for iterating over elements, and the product() function is imported to combine both tuples. Here, itertools.product() represents the Cartesian product, which means pairing each element from first_tuple with every element of second_tuple in a sequential manner and vice versa.
Example
In this example, we perform the task of 2-tuples to store the elements and pair them in every possible combination using itertools module:
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)
The above program produces the following result:
[(5, 5), (5, 6), (2, 5), (2, 6), (5, 5), (5, 2), (6, 5), (6, 2)]
Using List Comprehension
List comprehension is another way to create new lists in Python, and it can also be used to generate all possible pair combinations from two tuples.
Here are the steps to generate all pair combinations using list comprehension:
- Iterate through each element in the first tuple.
- For each element in the first tuple, iterate through every element in the second tuple.
- Form a pair (as a tuple) for each combination and collect them into a new list.
This results in a list of tuples representing all possible combinations of elements from both given tuples.
Example
In this example, we will see how to pairs elements of 2-tuples combination from two different variable:
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)Note: In this example, the + is used to combine every possible combination whereas \ define the line continuation character.
The above program produces the following result:
[[(6, 2), (6, 3), (7, 2), (7, 3)], (6, 2), (6, 3), (7, 2), (7, 3)]
Using Nested Loop
Python nested loop is a loop inside another loop. We can use it to generate all possible pair combinations from two tuples. Here are the steps to generate all pair combinations using nested loops:
- Use an outer loop to iterate through each element of the first tuple.
- Inside the outer loop, use an inner loop to iterate through each element of the second tuple.
- For every iteration, form a tuple with elements from both loops and store it.
This method gives a complete list of all pairwise combinations between the two tuples.
Example
Following is an example of a nested loop in which elements are iterating for every possible pair combination:
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)
The above program produces the following result:
[(10, 50), (10, 60), (20, 50), (20, 60), (50, 10), (50, 20), (60, 10), (60, 20)]
Using map() and lambda Function
The Python's map() function applies a specific operation to each item in an iterable. We can use the map() function with a lambda function and list comprehension to generate all possible combinations of two tuples. Here are the steps:
Example
In this example, we use map() and lambda to solve all pairs combinations of 2 tuples:
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)
The above program produces the following result:
[(1, 8), (1, 4), (7, 8), (7, 4), (8, 1), (8, 7), (4, 1), (4, 7)]
Conclusion
In Python, we can generate all possible pair combinations of two tuples using different methods. We used various approaches such as itertools module, list comprehension, nested loop, and map() with lambda to perform this task. All methods help to match each element from both tuples and give the same result.