Open In App

Python – All pair combinations of 2 tuples

Last Updated : 19 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with Python tuples data, we can have a problem in which we need to extract all possible combination of 2 argument tuples. This kind of application can come in Data Science or gaming domains. Let’s discuss certain ways in which this task can be performed.

Input : test_tuple1 = (7, 2), test_tuple2 = (7, 8) 
Output : [(7, 7), (7, 8), (2, 7), (2, 8), (7, 7), (7, 2), (8, 7), (8, 2)] 

Input : test_tuple1 = (9, 2), test_tuple2 = (7, 8) 
Output : [(9, 7), (9, 8), (2, 7), (2, 8), (7, 9), (7, 2), (8, 9), (8, 2)]

Method #1 : Using list comprehension This is one of the ways in which this task can be performed. In this, we perform task of forming one index combination in one pass, in other pass change the index, and add to the initial result list. 

Python3




# Python3 code to demonstrate working of
# All pair combinations of 2 tuples
# Using list comprehension
 
# initializing tuples
test_tuple1 = (4, 5)
test_tuple2 = (7, 8)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tuple1))
print("The original tuple 2 : " + str(test_tuple2))
 
# All pair combinations of 2 tuples
# Using list comprehension
res =  [(a, b) for a in test_tuple1 for b in test_tuple2]
res = res +  [(a, b) for a in test_tuple2 for b in test_tuple1]
 
# printing result
print("The filtered tuple : " + str(res))


Output : 

The original tuple 1 : (4, 5)
The original tuple 2 : (7, 8)
The filtered tuple : [(4, 7), (4, 8), (5, 7), (5, 8), (7, 4), (7, 5), (8, 4), (8, 5)]

Time complexity: O(n^2), where n is the length of the tuples. The nested loop results in iterating through each element in both tuples.
Auxiliary space: O(n^2), the resulting list ‘res’ has all pair combinations of the elements in the tuples.
 

Method #2 : Using chain() + product() The combination of above functions provide yet another way to solve this problem. In this, we perform task of pair creation using product() and chain() is used to add both the results from product() used twice. 

Python3




# Python3 code to demonstrate working of
# All pair combinations of 2 tuples
# Using chain() + product()
from itertools import chain, product
 
# initializing tuples
test_tuple1 = (4, 5)
test_tuple2 = (7, 8)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tuple1))
print("The original tuple 2 : " + str(test_tuple2))
 
# All pair combinations of 2 tuples
# Using chain() + product()
res = list(chain(product(test_tuple1, test_tuple2), product(test_tuple2, test_tuple1)))
 
# printing result
print("The filtered tuple : " + str(res))


Output : 

The original tuple 1 : (4, 5)
The original tuple 2 : (7, 8)
The filtered tuple : [(4, 7), (4, 8), (5, 7), (5, 8), (7, 4), (7, 5), (8, 4), (8, 5)]

Time complexity: O(2n^2), where n is the length of the tuples
Auxiliary space: O(n^2), as the result list stores all possible pair combinations of the two input tuples.

Method #3 : Using itertools.product(): 

Algorithm:

1.Initialize two tuples, “test_tuple1” and “test_tuple2”.
2.Create an empty list called “res”.
3.Use two nested loops to iterate through all pairs of elements in the two tuples.
4.For each pair, create a new tuple and append it to the “res” list.
5.Return the “res” list.
6.Print the resulting list.

Python3




import itertools
 
# initializing tuples
test_tuple1 = (4, 5)
test_tuple2 = (7, 8)
 
# printing original tuples
print("The original tuple 1 : " + str(test_tuple1))
print("The original tuple 2 : " + str(test_tuple2))
 
# generating all pair combinations of 2 tuples using list comprehension
res = [(a, b) for a in test_tuple1 for b in test_tuple2] + [(a, b) for a in test_tuple2 for b in test_tuple1]
 
# printing result
print("All pair combinations of 2 tuples : " + str(res))
#This code is contributed by Jyothi pinjala.


Output

The original tuple 1 : (4, 5)
The original tuple 2 : (7, 8)
All pair combinations of 2 tuples : [(4, 7), (4, 8), (5, 7), (5, 8), (7, 4), (7, 5), (8, 4), (8, 5)]

Time complexity: O(nm)
The time complexity of this algorithm is O(nm) because it uses two nested loops to iterate through all pairs of elements in the two input tuples, which takes n*m iterations where n and m are the lengths of the input tuples.

Auxiliary Space: O(nm)
The space complexity of this algorithm is also O(nm) because a new list is created to store the result, and the size of the list is proportional to the number of pairs generated.

METHOD 4:Using nested loops

APPROACH:

The above program generates all possible pairs of elements from two given tuples, and stores them in a filtered list. The filtered list includes both the tuples with the first element from the first tuple and the second element from the second tuple, and the tuples with the first element from the second tuple and the second element from the first tuple.

ALGORITHM:

1.Initialize an empty list to store the filtered tuples.
2.Use nested loops to iterate over each element in tuple 1 and tuple 2.
3.Append a tuple of the two elements to the filtered list.
4.Append a tuple of the two elements in reverse order to the filtered list.
5.Output the filtered list.

Python3




# input
tuple1 = (4, 5)
tuple2 = (7, 8)
 
# initialize an empty list to store the filtered tuples
filtered_tuples = []
 
# iterate over each element in tuple 1
for element1 in tuple1:
    # iterate over each element in tuple 2
    for element2 in tuple2:
        # append a tuple of the two elements to the filtered list
        filtered_tuples.append((element1, element2))
        # append a tuple of the two elements in reverse order to the filtered list
        filtered_tuples.append((element2, element1))
 
# output
print(filtered_tuples)


Output

[(4, 7), (7, 4), (4, 8), (8, 4), (5, 7), (7, 5), (5, 8), (8, 5)]

Time complexity: The time complexity of this program is O(n^2), where n is the length of the tuples. This is because the program uses nested loops to iterate over each element in the two tuples.

Space complexity: The space complexity of this program is O(n^2), as the filtered list will contain all possible pairs of elements from the two tuples. The exact space complexity will depend on the length of the tuples.



Similar Reads

itertools.combinations() module in Python to print all possible combinations
Given an array of size n, generate and print all possible combinations of r elements in array. Examples: Input : arr[] = [1, 2, 3, 4], r = 2 Output : [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]Recommended: Please try your approach on {IDE} first, before moving on to the solution. This problem has existing recursive solution please refer Print
2 min read
Python - Filter all uppercase characters Tuples from given list of tuples
Given a Tuple list, filter tuples that contain all uppercase characters. Input : test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [('GFG', 'IS', 'BEST')] Explanation : Only 1 tuple has all uppercase Strings. Input : test_list = [("GFG", "iS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [
8 min read
Python program to find tuples which have all elements divisible by K from a list of tuples
Given a list of tuples. The task is to extract all tuples which have all elements divisible by K. Input : test_list = [(6, 24, 12), (60, 12, 6), (12, 18, 21)], K = 6 Output : [(6, 24, 12), (60, 12, 6)] Explanation : Both tuples have all elements multiple of 6. Input : test_list = [(6, 24, 12), (60, 10, 5), (12, 18, 21)], K = 5 Output : [(60, 10, 5)
7 min read
Python - Combinations of sum with tuples in tuple list
Sometimes, while working with data, we can have a problem in which we need to perform tuple addition among all the tuples in list. This can have applications in many domains. Let’s discuss certain ways in which this task can be performed. Method #1: Using combinations() + list comprehension This problem can be solved using combinations of the above
4 min read
Python | Remove duplicate tuples from list of tuples
Given a list of tuples, Write a Python program to remove all the duplicated tuples from the given list. Examples: Input : [(1, 2), (5, 7), (3, 6), (1, 2)] Output : [(1, 2), (5, 7), (3, 6)] Input : [('a', 'z'), ('a', 'x'), ('z', 'x'), ('a', 'x'), ('z', 'x')] Output : [('a', 'z'), ('a', 'x'), ('z', 'x')] Method #1 : List comprehension This is a naive
5 min read
Python | Find the tuples containing the given element from a list of tuples
Given a list of tuples, the task is to find all those tuples containing the given element, say n. Examples: Input: n = 11, list = [(11, 22), (33, 55), (55, 77), (11, 44)] Output: [(11, 22), (11, 44)] Input: n = 3, list = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] Output: [(14, 3), (1, 3), (3, 3)] There are multiple ways we can find the tuples contai
6 min read
Python | Remove tuples from list of tuples if greater than n
Given a list of a tuple, the task is to remove all the tuples from list, if it's greater than n (say 100). Let's discuss a few methods for the same. Method #1: Using lambda STEPS: Initialize a list of tuples: ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]Print the initial list: print("intial_list", str(ini_tuple))Define the
6 min read
Python | Remove tuples having duplicate first value from given list of tuples
Given a list of tuples, the task is to remove all tuples having duplicate first values from the given list of tuples. Examples: Input: [(12.121, 'Tuple1'), (12.121, 'Tuple2'), (12.121, 'Tuple3'), (923232.2323, 'Tuple4')] Output: [(12.121, 'Tuple1'), (923232.2323, 'Tuple4')]Input: [('Tuple1', 121), ('Tuple2', 125), ('Tuple1', 135), ('Tuple4', 478)]
7 min read
Python | Count tuples occurrence in list of tuples
Many a time while developing web and desktop products in Python, we use nested lists and have several queries about how to find the count of unique tuples. Let us see how to get the count of unique tuples in the given list of tuples. Below are some ways to achieve the above task. Method #1: Using Iteration C/C++ Code # Python code to count unique #
5 min read
Python | Combining tuples in list of tuples
Sometimes, we might have to perform certain problems related to tuples in which we need to segregate the tuple elements to combine with each element of complex tuple element( such as list ). This can have application in situations we need to combine values to form a whole. Let's discuss certain ways in which this can be performed. Method #1: Using
7 min read