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.

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

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions