Skip to content
Related Articles

Related Articles

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

View Discussion
Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 10 Apr, 2019
View Discussion
Improve Article
Save Article

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 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 :

Start Your Coding Journey Now!