Python all pair combinations of 2 tuples

Sometimes, while working with Python tuples data, we can have a problem in which we need to extract all possible combination of 2 argument tuples. This kind of application can come in Data Science or gaming domains. Let’s discuss certain ways in which this task can be performed.

Input : test_tuple1 = (7, 2), test_tuple2 = (7, 8)
Output : [(7, 7), (7, 8), (2, 7), (2, 8), (7, 7), (7, 2), (8, 7), (8, 2)]

Input : test_tuple1 = (9, 2), test_tuple2 = (7, 8)
Output : [(9, 7), (9, 8), (2, 7), (2, 8), (7, 9), (7, 2), (8, 9), (8, 2)]

Method #1 : Using list comprehension
This is one of the ways in which this task can be performed. In this, we perform task of forming one index combination in one pass, in other pass change the index, and add to the initial result list.

test_tuple1 = (4, 5)

test_tuple2 = (7, 8)

print("The original tuple 1 : " + str(test_tuple1))

print("The original tuple 2 : " + str(test_tuple2))

res =  [(a, b) for a in test_tuple1 for b in test_tuple2]

res = res +  [(a, b) for a in test_tuple2 for b in test_tuple1]

print("The filtered tuple : " + str(res))

Output :

The original tuple 1 : (4, 5)
The original tuple 2 : (7, 8)
The filtered tuple : [(4, 7), (4, 8), (5, 7), (5, 8), (7, 4), (7, 5), (8, 4), (8, 5)]

Method #2 : Using chain() + product()
The combination of above functions provide yet another way to solve this problem. In this, we perform task of pair creation using product() and chain() is used to add both the results from product() used twice.

from itertools import chain, product

test_tuple1 = (4, 5)

test_tuple2 = (7, 8)

print("The original tuple 1 : " + str(test_tuple1))

print("The original tuple 2 : " + str(test_tuple2))

res = list(chain(product(test_tuple1, test_tuple2), product(test_tuple2, test_tuple1)))

print("The filtered tuple : " + str(res))

Output :

The original tuple 1 : (4, 5)
The original tuple 2 : (7, 8)
The filtered tuple : [(4, 7), (4, 8), (5, 7), (5, 8), (7, 4), (7, 5), (8, 4), (8, 5)]


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.

How do I combine two tuples?

When it is required to concatenate multiple tuples, the '+' operator can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements.

Can two tuples be concatenated together?

Operators can be used to concatenate or multiply tuples. Concatenation is done with the + operator, and multiplication is done with the * operator. Because the + operator can concatenate, it can be used to combine tuples to form a new tuple, though it cannot modify an existing tuple.

Can we compare 2 tuples in Python?

Practical Data Science using Python Tuples are compared position by position: the first item of the first tuple is compared to the first item of the second tuple; if they are not equal, this is the result of the comparison, else the second item is considered, then the third and so on.

How do you print tuple pairs?

Simply pass the tuple into the print function to print tuple values. It will print the exact tuple but if you want to print it with string then convert it to string.