Python | Get first element with maximum value in list of tuples
Last Updated :
17 Apr, 2023
In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let's discuss certain ways in which this can be achieved.
Method #1 : Using max() + operator.itemgetter() We can get the maximum of corresponding tuple index from a list using the key itemgetter index provided and then mention the index information required using index specification at the end.
Python3
# Python3 code to demonstrate
# to get tuple info. of maximum value tuple
# using max() + itemgetter()
from operator import itemgetter
# initializing list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
# printing original list
print ("Original list : " + str(test_list))
# using max() + itemgetter()
# to get tuple info. of maximum value tuple
res = max(test_list, key = itemgetter(1))[0]
# printing result
print ("The name with maximum score is : " + res)
Output:Original list : [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
The name with maximum score is : Manjeet
Time Complexity: O(n), where n is the length of the input list. This is because we’re using max() + operator.itemgetter() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.
Method #2 : Using max() + lambda This method is almost similar to the method discussed above, just the difference is the specification and processing of target tuple index for maximum is done by lambda function. This improved readability of code.
Python3
# Python3 code to demonstrate
# to get tuple info. of maximum value tuple
# using max() + lambda
# initializing list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
# printing original list
print ("Original list : " + str(test_list))
# using max() + lambda
# to get tuple info. of maximum value tuple
res = max(test_list, key = lambda i : i[1])[0]
# printing result
print ("The name with maximum score is : " + res)
Output:Original list : [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
The name with maximum score is : Manjeet
Method #3 : Using sorted() + lambda The task performed by the max() in the above two methods can be done using reverse sorting and printing the first element. lambda function performs the task similar to above said methods.
Python3
# Python3 code to demonstrate
# to get tuple info. of maximum value tuple
# using sorted() + lambda
# initializing list
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
# printing original list
print ("Original list : " + str(test_list))
# using sorted() + lambda
# to get tuple info. of maximum value tuple
res = sorted(test_list, key = lambda i: i[1], reverse = True)[0][0]
# printing result
print ("The name with maximum score is : " + res)
Output:Original list : [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
The name with maximum score is : Manjeet
Method #4 : Using heapq module
One approach is using the heapq module. The heapq module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.
heapq is a python module that provides an implementation of the heap queue algorithm, also known as the priority queue algorithm. It allows you to efficiently find and extract the largest or smallest elements from a list, as well as insert new elements into the list in the correct order.
The nlargest function from heapq allows you to find the n largest elements from a list. It returns a list of the n largest elements in descending order.
Here is an example of how to use the heapq module to get the first element with the maximum value in a list of tuples:
Python3
import heapq
test_list = [('Rash', 143), ('Manjeet', 200), ('Varsha', 100)]
# Negate the values in the tuples to turn them into minimums
# and then use heapq.nlargest to get the smallest element
res = heapq.nlargest(1, [(v, k) for k, v in test_list])[0][1]
print("The name with the maximum score is:", res)
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe name with the maximum score is: Manjeet
This approach has a time complexity of O(n log n) and a space complexity of O(n), where n is the number of tuples in the list.
Similar Reads
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 :
5 min read
Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read
Python - Get maximum of Nth column from tuple list Sometimes, while working with Python lists, we can have a task in which we need to work with tuple list and get the maximum of its Nth index. This problem has application in web development domain while working with data information. Letâs discuss certain ways in which this task can be performed. Me
7 min read
Min and Max value in list of tuples-Python The task of finding the minimum and maximum values in a list of tuples in Python involves identifying the smallest and largest elements from each position (column) within the tuples. For example, given [(2, 3), (4, 7), (8, 11), (3, 6)], the first elements (2, 4, 8, 3) have a minimum of 2 and a maxim
3 min read
Python | Find the sublist with maximum value in given nested list Given a list of list, the task is to find sublist with the maximum value in second column. Examples: Input : [['Paras', 90], ['Jain', 32], ['Geeks', 120], ['for', 338], ['Labs', 532]] Output :['Labs', 532] Input: [['Geek', 90], ['For', 32], ['Geeks', 120]] Output: ['Geeks', 120] Below are some tasks
4 min read