Python – All pair combinations of 2 tuples
Last Updated : 19 Apr, 2023
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
test_tuple1 = ( 4 , 5 ) test_tuple2 = ( 7 , 8 ) print ("The original tuple 1 : " + str (test_tuple1)) print ("The original tuple 2 : " + str (test_tuple2)) 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] 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
from itertools import chain, product test_tuple1 = ( 4 , 5 ) test_tuple2 = ( 7 , 8 ) print ("The original tuple 1 : " + str (test_tuple1)) print ("The original tuple 2 : " + str (test_tuple2)) res = list (chain(product(test_tuple1, test_tuple2), product(test_tuple2, test_tuple1))) 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 test_tuple1 = ( 4 , 5 ) test_tuple2 = ( 7 , 8 ) print ( "The original tuple 1 : " + str (test_tuple1)) print ( "The original tuple 2 : " + str (test_tuple2)) 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] print ( "All pair combinations of 2 tuples : " + str (res)) |
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
tuple1 = ( 4 , 5 ) tuple2 = ( 7 , 8 ) filtered_tuples = [] for element1 in tuple1: for element2 in tuple2: filtered_tuples.append((element1, element2)) filtered_tuples.append((element2, element1)) 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
Python | All possible N combination tuples
Sometimes, while working with Python tuples, we might have a problem in which we need to generate all possible combination pairs till N. This can have application in mathematics domain. Let's discuss certain ways in which this problem can be solved. Method #1 : Using list comprehension + product() T
5 min read
Python List and Tuple Combination Programs
Lists and tuples are two of the most commonly used data structures in Python. While lists are mutable and allow modifications, tuples are immutable and provide a stable structure for storing data. This article explores various programs related to list and tuple combinations, covering topics like: So
6 min read
Print a List of Tuples in Python
The task of printing a list of tuples in Python involves displaying the elements of a list where each item is a tuple. A tuple is an ordered collection of elements enclosed in parentheses ( ), while a list is an ordered collection enclosed in square brackets [ ]. Using print()print() function is the
2 min read
Python | Addition of tuples
Sometimes, while working with records, we might have a common problem of adding contents of one tuple with the corresponding index of other tuple. This has application in almost all the domains in which we work with tuple records. Let's discuss certain ways in which this task can be performed. Metho
5 min read
Python - Pairwise Addition in Tuples
Sometimes, while working with data, we can have a problem in which we need to find cumulative result. This can be of any type, product or summation. Here we are gonna discuss about adjacent element addition. Letâs discuss certain ways in which this task can be performed. Method #1 : Using zip() + ge
4 min read
Python - All replacement combination from other list
Given a list, the task is to write a Python program to perform all possible replacements from other lists to the current list. Input : test_list = [4, 1, 5], repl_list = [8, 10] Output : [(4, 1, 5), (4, 1, 8), (4, 1, 10), (4, 5, 8), (4, 5, 10), (4, 8, 10), (1, 5, 8), (1, 5, 10), (1, 8, 10), (5, 8, 1
3 min read
Python program to get all unique combinations of two Lists
The goal is to combine each item from first list with each item from second list in every possible unique way. If we want to get all possible combinations from two lists. Pythonâs itertools library has a function called a product that makes it easy to generate combinations of items from multiple lis
2 min read
Python | Pair Product combinations
Sometimes, while working with data, we can have a problem in which we need to perform tuple multiplication among all the tuples in list. This can have application in many domains. Letâs discuss certain ways in which this task can be performed. Method #1 : Using combinations() + list comprehension Th
4 min read
Python | Pair and combine nested list to tuple list
Sometimes we need to convert between the data types, primarily due to the reason of feeding them to some function or output. This article solves a very particular problem of pairing like indices in list of lists and then construction of list of tuples of those pairs. Let's discuss how to achieve the
10 min read
Python - All combination Dictionary List
Given 2 lists, form all possible combination of dictionaries that can be formed by taking keys from list1 and values from list2. Input : test_list1 = ["Gfg", "is", "Best"], test_list2 = [4] Output : [{'Gfg': 4, 'is': 4, 'Best': 4}]Explanation : All combinations dictionary list extracted. Input : tes
3 min read