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.
# 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)) |
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().
# 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)) |
The original dictionary is : {'gfg': (4, 6), 'is': (7, 8), 'best': (8, 2)}
The extracted maximum element item : ('is', (7, 8))
Recommended Posts:
- Python - Extract ith Key's Value of K's Maximum value dictionary
- Python - Extract tuple supersets from List
- Python - Extract digits from Tuple list
- Python - Extract Even elements in Nested Mixed Tuple
- Python - Extract Kth element of every Nth tuple in List
- Python Regex to extract maximum numeric value from a string
- Python - Extract Maximum Keys' value dictionaries
- Python | Sort tuple list by Nth element of tuple
- Python | Replace tuple according to Nth tuple element
- Python - Raise elements of tuple as power to another tuple
- Python - Convert Tuple String to Integer Tuple
- Python - Convert Tuple to Tuple Pair
- Python - Flatten tuple of List to tuple
- Python - Convert Tuple Matrix to Tuple List
- Python - Maximum value in record list as tuple attribute
- Python | Extract key-value of dictionary in variables
- Python - Extract Unique value key pairs
- Python - Extract Kth index elements from Dictionary Value list
- Python - Extract Key's value from Mixed Dictionaries List
- Python - Extract ith element of K key's value
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.

