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 method in python. Let's see them in working...
Method 1:
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:
Output:
Method 2:
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:
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer