Home »
Python »
Python Programs
Python program to find all pairs combination of two tuples
Here, we have two tuples and we need to find all the pair combinations of the two tuples in Python programming language.
By Shivang Yadav Last updated : December 12, 2023
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.
Example:
tuple = ("python", "includehelp", 43, 54.23)
Finding All Pairs Combination of Two Tuples
Finding all possible combinations is a commonly used task in AI and gaming to extract all possible results from the given input. Here, we will see how to find all combinations which you can later apply in programming.
We have two tuples with elements in them and we need to find all pair combinations from the elements of these two tuples i.e. taking one element from each tuple.
Example
Input:
tup1 = (1, 2), tup2 = (5, 7)
Output:
[(1, 5), (1, 7), (2, 5), (2, 7), (5, 1), (5, 2), (7, 1), (7, 2)]
Python programming language provides us multiple ways to perform this task using loops or by using some built-in methods in Python. Let's see them in working...
Method 1: Iterating over the tuples
A simple method is to iterate over the tuples. For one, take elements from another to make pairs and then repeat the process that elements from other tuples first. This uses nested loops.
Program
# Program to Find All Pairs Combination of Two Tuples
# Creating Tuples and printing values
tuple1 = (1, 4)
tuple2 = (3, 9)
print("First tuple : " + str(tuple1))
print("Second tuple : " + str(tuple2))
# finding all pair Combination of tuples
pairCombi = []
for val1 in tuple1:
for val2 in tuple2:
tup = [val1, val2]
pairCombi.append(tuple(tup))
for val1 in tuple2:
for val2 in tuple1:
tup = [val1, val2]
pairCombi.append(tuple(tup))
# Printing tuple Combination
print("All pair Combinations are : " + str(pairCombi))
Output:
First tuple : (1, 4)
Second tuple : (3, 9)
All pair Combinations are : [(1, 3), (1, 9), (4, 3), (4, 9), (3, 1), (3, 4), (9, 1), (9, 4)]
Method 2: Using product() method from the itertools library
One more method to find the pair combination is using the product() method from the itertools library. Using this we can be used to find all pairs of combinations and then we will add the pair combination to a list which will be the required values.
Program
# Program to Find All Pairs Combination of Two Tuples
import itertools
# Creating Tuples and printing values
tuple1 = (1, 4)
tuple2 = (3, 9)
print("First tuple : " + str(tuple1))
print("Second tuple : " + str(tuple2))
# finding all pair Combination of tuples
pairCombi = (list(itertools.product(tuple1, tuple2)))
pairCombi += (list(itertools.product(tuple2, tuple1)))
# Printing tuple Combination
print("All pair Combinations are : " + str(pairCombi))
Output:
First tuple : (1, 4)
Second tuple : (3, 9)
All pair Combinations are : [(1, 3), (1, 9), (4, 3), (4, 9), (3, 1), (3, 4), (9, 1), (9, 4)]
Python Tuple Programs »