
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Sublist with Maximum Value in Nested List in Python
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]
Advertisements