Python | Maximum of Product Pairs in Tuple List

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().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Maximum of Product Pairs in Tuple List
# Using list comprehension + max()
  
# initialize list
test_list = [(3, 5), (1, 7), (10, 3), (1, 2)]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Maximum of Product Pairs in Tuple List
# Using list comprehension + max()
temp = [abs(b * a) for a, b in test_list]
res = max(temp)
  
# printing result
print("Maximum product among pairs : " + str(res))

chevron_right


Output :

The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum product among pairs : 30

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Maximum of Product Pairs in Tuple List
# Using lambda + max()
  
# initialize list
test_list = [(3, 5), (1, 7), (10, 3), (1, 2)]
  
# printing original list 
print("The original list : " + str(test_list))
  
# Maximum of Product Pairs in Tuple List
# Using lambda + max()
res = max(test_list, key = lambda sub: sub[1] * sub[0])
  
# printing result
print("Maximum Product among pairs : " + str(res))

chevron_right


Output :

The original list : [(3, 5), (1, 7), (10, 3), (1, 2)]
Maximum product among pairs : 30



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.