Python | Maximum of Product Pairs in Tuple List
Last Updated :
18 May, 2023
Sometimes, while working with data, we might have a problem in which we need to find maximum product between available pairs in list. This can be application to many problems in mathematics domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using max() + list comprehension The combination of this functions can be used to perform this task. In this, we compute the product of all pairs and then return the max of it using max().
Python3
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )]
print ("The original list : " + str (test_list))
temp = [ abs (b * a) for a, b in test_list]
res = max (temp)
print ("Maximum product among pairs : " + str (res))
|
Output :
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum product among pairs : 30
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is required
Method #2 : Using max() + lambda This is similar to above method. In this the task performed by list comprehension is solved using lambda function, providing the product computation logic. Returns the max. product pair.
Python3
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )]
print ("The original list : " + str (test_list))
res = max (test_list, key = lambda sub: sub[ 1 ] * sub[ 0 ])
print ("Maximum Product among pairs : " + str (res))
|
Output :
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum product among pairs : 30
Method 3 : Here is another approach using the reduce function from the functools library:
Python3
from functools import reduce
from operator import mul
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )]
print ( "The original list : " + str (test_list))
result = reduce ( lambda x, y: x if x[ 1 ] * x[ 0 ] > y[ 1 ] * y[ 0 ] else y, test_list)
print ( "Maximum Product among pairs : " + str (result[ 0 ] * result[ 1 ]))
|
Output
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum Product among pairs : 30
This code uses the reduce function to reduce the list to a single tuple which has the maximum product. The lambda function takes in two tuples and compares their product, returning the tuple with the larger product. This process is repeated until a single tuple with the maximum product is left.
Time complexity: O(n)
Space complexity: O(1)
Method #4: Using a loop and comparison
Initialize two variables, max_product and current_product, to 0.
Loop through each tuple in the list.
Calculate the product of the tuple elements and store it in current_product.
If current_product is greater than max_product, set max_product to current_product.
After the loop ends, print the value of max_product.
Python3
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )]
print ( "The original list : " + str (test_list))
max_product = 0
for sub in test_list:
current_product = sub[ 1 ] * sub[ 0 ]
if current_product > max_product:
max_product = current_product
print ( "Maximum Product among pairs : " + str (max_product))
|
Output
The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum Product among pairs : 30
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(1)
Method #5 : Using the itertools module
- Initialize the test_list variable with a list of tuples representing the input data.
- Print the original list using print(“The original list: ” + str(test_list)).
- Initialize the max_product variable with negative infinity using float(‘-inf’). This variable will store the maximum product among pairs.
- Iterate over all possible pairs of tuples from test_list using itertools.combinations(test_list, 2).
- For each pair of tuples, multiply the first elements of the pair using pair[0][0] and pair[1][0] and store the result in the product variable.
- Compare the product with the current max_product. If the product is greater than max_product, update max_product with the new value.
- After iterating through all pairs and finding the maximum product, the program prints the result using print(“Maximum Product among pairs: ” + str(max_product)).
Python3
import itertools
test_list = [( 3 , 5 ), ( 1 , 7 ), ( 10 , 3 ), ( 1 , 2 )]
print ( "The original list: " + str (test_list))
max_product = float ( '-inf' )
for pair in itertools.combinations(test_list, 2 ):
product = pair[ 0 ][ 0 ] * pair[ 1 ][ 0 ]
if product > max_product:
max_product = product
print ( "Maximum Product among pairs: " + str (max_product))
|
Output
The original list: [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum Product among pairs: 30
Time complexity: O(n^2).
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...