Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Find the sublist with maximum value in given nested list

  • Difficulty Level : Expert
  • Last Updated : 10 Apr, 2019

Given a list of list, the task is to find sublist with the maximum value in second column.

Examples:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

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 to achieve the above task.



Method #1: Using lambda




# Python code to find maximum value 
# in second column of list of list
  
# Input list initialization
Input = [['Paras', 90], ['Jain', 32], ['Geeks', 120],
                        ['for', 338], ['Labs', 532]]
# Using lambda 
Output = max(Input, key = lambda x: x[1])
  
# printing output
print("Input List is :", Input)
print("Output list is : ", Output)
Output:

Input List is : [[‘Paras’, 90], [‘Jain’, 32], [‘Geeks’, 120], [‘for’, 338], [‘Labs’, 532]]
Output list is : [‘Labs’, 532]

 
Method #2: Using itemgetter




# Python code to find maximum value 
# in second column of list of list
  
# Importing
import operator 
  
# Input list initialization
Input = [['Paras', 90], ['Jain', 32], ['Geeks', 120],
                        ['for', 338], ['Labs', 532]]
# Using itemgetter
Output = max(Input, key = operator.itemgetter(1))
  
# Printing output
print("Input List is :", Input)
print("Output list is : ", Output)
Output:

Input List is : [[‘Paras’, 90], [‘Jain’, 32], [‘Geeks’, 120], [‘for’, 338], [‘Labs’, 532]]
Output list is : [‘Labs’, 532]




My Personal Notes arrow_drop_up
Recommended Articles
Page :