Q:

Python program to find all pairs combination of two tuples

belongs to collection: Python Tuple Programs

0

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...

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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:

# 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:

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)]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to join tuples if similar initial e... >>
<< Python program to extract digits from tuple list...