Friday, December 27, 2024
Google search engine
HomeLanguagesPython – All pair combinations of 2 tuples

Python – All pair combinations of 2 tuples

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.

RELATED ARTICLES

Most Popular

Recent Comments