Signup/Sign In
PUBLISHED ON: AUGUST 11, 2021

Python Program to print all pair combinations of 2 tuples

When working with Python tuples data, we may encounter a situation where we need to extract every conceivable combination of two-parameter tuples. This type of application may be found in the fields of data science and gaming.

In this tutorial, we will learn All pair combinations of 2 tuples in two different approaches.

Let us first look at the example to understand the input-output format:

Input: tuple1 = (1, 2), tuple2 = (3, 4)

Output: [(1, 3), (1, 4), (2, 3), (2, 4), (3, 1), (3, 2), (4, 1), (4, 2)]

Input: tuple1 = (4, 5), tuple2 = (6, 7)

Output: [(4, 6), (4, 7), (5, 6), (5, 7), (6, 4), (6, 5), (7, 4), (7, 5)]

To execute this task, we can follow multiple approaches, some are discussed below:

  1. Using Chain and Product function

  2. Using list comprehension

We will discuss all these approaches in detail separately.

Approach 1: Using Chain and Product function

The first method to tackle this problem is to use the combination of chain and product. In this case, we utilize the product() to execute the work of pair formation, and chain() is used to combine both of the product() outputs.

Algorithm

  1. From itertool import chain
  2. From itertool import product
  3. Initialize tuples
  4. Find all pair combinations of two tuples using chain() and product()
  5. Print Out the result

Program of print all pair combinations of 2 tuples

To discover all the digit combinations, we utilized the product() and chain() methods. The pair combinations are found using the product function, and the result is combined using the chain() function.

from itertools import chain, product 
#  tuples
tuple1 = (1, 2)
tuple2 = (3, 4) 
#  original tuples
print("The tuple 1 : " + str(tuple1))
print("The tuple 2 : " + str(tuple2)) 
# All pair combinations of 2 tuples
result = list(chain(product(tuple1, tuple2), product(tuple2, tuple1))) 
#  result 
print("The resultant tuple : " + str(result))


The tuple 1 : (1, 2)
The tuple 2 : (3, 4)
The resultant tuple : [(1, 3), (1, 4), (2, 3), (2, 4), (3, 1), (3, 2), (4, 1), (4, 2)]

Approach 2: Using List Comprehensions

We used list comprehension to pair one index digit and then built another pair combination with another index in this technique. On the screen, both outputs are merged and printed.

Algorithm

  1. Initializing tuples
  2. Printing the original tuples
  3. Find all pair combinations of two tuples using chain() and product()
  4. Print the result

Program To print all pair combinations of 2 tuples

This is one approach to completing the assignment. In this case, we construct one index combination in one pass, update the index in the next pass, and add to the resulting list in the final pass.

tuple1 = (4, 5)
tuple2 = (6, 7)
#  original tuples
print("The tuple 1 : " + str(tuple1))
print("The tuple 2 : " + str(tuple2))
# All pair combinations of 2 tuples
result =  [(x, y) for x in tuple1 for y in tuple2]
result = result +  [(x, y) for x in tuple2 for y in tuple1]
  
#  result 
print("The resultant tuple : " + str(result))


The tuple 1 : (4, 5)
The tuple 2 : (6, 7)
The resultant tuple : [(4, 6), (4, 7), (5, 6), (5, 7), (6, 4), (6, 5), (7, 4), (7, 5)]

Conclusion

In this tutorial, we have seen two approaches to find all pairs combinations of 2 tuples. The first approach is by using the chain and product function method in which we utilize the product() to execute the work of pair formation, and chain() is used to combine both of the product() outputs and the second approach is by using list comprehensions in which we construct one index combination in one pass, update the index in the next pass, and add to the initial result list in the third run.



About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.