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

Method #1: Using lambda

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

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

 
Method #2: Using itemgetter

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


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


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.