ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

# 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.
Submitted by Shivang Yadav, on June 08, 2021

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

```# 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)]
```
ADVERTISEMENT
ADVERTISEMENT

Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.