Python – Extract Item with Maximum Tuple Value

Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract the item with maximum value of value tuple index. This kind of problem can have application in domains such as web development. Let’s discuss certain ways in which this task can be performed.

Input : test_dict = {‘gfg’ : (4, 6), ‘is’ : (7, 8), ‘best’ : (8, 2)}, tup_idx = 0
Output : (‘best’, (8, 2))

Input : test_dict = {‘gfg’ : (4, 6), ‘best’ : (8, 2)}, tup_idx = 1
Output : (‘gfg’ : (4, 6))

Method #1 : Using max() + lambda
The combination of above functions can be used to solve this problem. In this, we perform the task of extracting maximum item using max, and value parameter is checked using lambda.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract Item with Maximum Tuple Value
# Using max() + lambda
  
# initializing dictionary
test_dict = {'gfg' : (4, 6),
             'is' : (7, 8),
             'best' : (8, 2)}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing tuple index 
# 0 based indexing
tup_idx = 1
  
# Extract Item with Maximum Tuple Value
# Using max() + lambda
res = max(test_dict.items(), key = lambda ele: ele[1][tup_idx])
      
# printing result 
print("The extracted maximum element item : " + str(res)) 

chevron_right


Output :



The original dictionary is : {'gfg': (4, 6), 'is': (7, 8), 'best': (8, 2)}
The extracted maximum element item : ('is', (7, 8))

 

Method #2: Using max() + map() + itemgetter() + zip()
The combination of above functions can be used to solve this problem. In this, we perform the task of zipping key and required tuple index value extracted using itemgetter() using zip(). Then maximum element is extracted using max().

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of 
# Extract Item with Maximum Tuple Value
# Using max() + map() + itemgetter() + zip()
from operator import itemgetter
  
# initializing dictionary
test_dict = {'gfg' : (4, 6),
             'is' : (7, 8),
             'best' : (8, 2)}
  
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
  
# initializing tuple index 
# 0 based indexing
tup_idx = 1
  
# Extract Item with Maximum Tuple Value
# Using max() + map() + itemgetter() + zip()
res = max(zip(test_dict.keys(), map(itemgetter(tup_idx), inventory.values())), key = itemgetter(1))[0]
res = (res, test_dict[res])
  
# printing result 
print("The extracted maximum element item : " + str(res)) 

chevron_right


Output :

The original dictionary is : {'gfg': (4, 6), 'is': (7, 8), 'best': (8, 2)}
The extracted maximum element item : ('is', (7, 8))



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.