Monday, November 18, 2024
Google search engine
HomeLanguagesPython | Find the sublist with maximum value in given nested list

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 

Python3




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

Time Complexity: O(n log n) – where n is the number of elements in the input list, as the max function applies the key function to each element of the input list and then performs a comparison between elements to determine the maximum
Auxiliary Space Complexity: O(1)

Method #2: Using itemgetter 

Python3




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

Time Complexity: O(n log n) where n is the number of elements in the Input list. This is because the max() function sorts the list of lists to find the maximum value. The sorting operation takes O(n log n) time.
Auxiliary Space: O(1), because we’re only storing the result of the max() function, which has a constant size of 1.

Method #3 : Using max() and for loop

Python3




# 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]]
 
x=[]
for i in Input:
    x.append(i[1])
ma=max(x)
for i in Input:
    if ma in i:
        Output=i
        break
# 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]

Time Complexity: O(n*n), where n is the length of the list test_dict
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list

Method #4 : Using reduce

To find the sublist with the maximum value in the second column of a given nested list using the reduce function from the functools module, you can use the following approach:

Python3




from functools import reduce
 
# Input list initialization
input_list = [['Paras', 90], ['Jain', 32], ['Geeks', 120], ['for', 338], ['Labs', 532]]
 
# Using reduce
output = reduce(lambda x, y: x if x[1] > y[1] else y, input_list)
 
# Printing output
print("Input List is :", input_list)
print("Output list is : ", output)
#This code is contributed by Edula Vinay Kumar Reddy


Output

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

This will output [‘Labs’, 532], which is the sublist with the maximum value in the second column.

Note: The reduce function applies a function to each element of the list in a cumulative manner, starting from the first element and moving to the last element. In this case, the function compares the second element of each sublist (the value in the second column) and returns the sublist with the maximum value.

Time complexity: O(n)
Auxiliary space: O(1)

Method 5: using the built-in function sorted() with a custom sorting key.

Python3




Input = [['Paras', 90], ['Jain', 32], ['Geeks', 120], ['for', 338], ['Labs', 532]]
 
# Sort the input list in descending order based on the second element of each sublist
sorted_list = sorted(Input, key=lambda x: x[1], reverse=True)
 
# The first element of the sorted list will be the sublist with the maximum second element
max_value_sublist = sorted_list[0]
 
print("Input List is :", Input)
print("Max value sublist is : ", max_value_sublist)


Output

Input List is : [['Paras', 90], ['Jain', 32], ['Geeks', 120], ['for', 338], ['Labs', 532]]
Max value sublist is :  ['Labs', 532]

Time complexity: O(n log n), where n is the length of the input list.
Auxiliary space: O(n) to store the sorted list.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments