A list can contain other list as its elements. In this article we are equal to find the sublist with maximum value which are present in a given list.
With max and lambda
The max and the Lambda function can together be used to give that sublist which has the maximum value.
Example
listA = [['Mon', 90], ['Tue', 32], ['Wed', 120]] # Using lambda res = max(listA, key=lambda x: x[1]) # printing output print("Given List:\n", listA) print("List with maximum value:\n ", res)
Output
Running the above code gives us the following result −
Given List: [['Mon', 90], ['Tue', 32], ['Wed', 120]] List with maximum value: ['Wed', 120]
With itergetter
We use itemgetter from index position 1 and apply a max function to get the sublist with maximum value.
Example
import operator listA = [['Mon', 90], ['Tue', 32], ['Wed', 120]] # Using itemgetter res = max(listA, key = operator.itemgetter(1)) # printing output print("Given List:\n", listA) print("List with maximum value:\n ", res)
Output
Running the above code gives us the following result −
Given List: [['Mon', 90], ['Tue', 32], ['Wed', 120]] List with maximum value: ['Wed', 120]