Python | Find the sublist with maximum value in given nested list
Last Updated :
27 Apr, 2023
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)
OutputInput 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
OutputInput 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)
OutputInput 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.
Similar Reads
Python - Find maximum length sub-list in a nested list
In Python, we often work with nested lists (lists inside lists), and sometimes we need to find out which sub-list has the most items. In this article, we will explore Various methods to Find the maximum length of a sub-list in a nested list. Using max() Function with key=lenThe simplest and most eff
2 min read
Python | Get first element with maximum value in list of tuples
In Python, we can bind structural information in form of tuples and then can retrieve the same. But sometimes we require the information of tuple corresponding to maximum value of other tuple indexes. This functionality has many applications such as ranking. Let's discuss certain ways in which this
4 min read
Python - Find the Maximum of Similar Indices in two list of Tuples
Sometimes, while working with Python records, we can have a problem in which we need to perform cross maximization of list of tuples. This kind of application is popular in web development domain. Letâs discuss certain ways in which this task can be performed. Method #1 : Using list comprehension +
7 min read
Python - Get the object with the max attribute value in a list of objects
Given a list of objects, the task is to write a Python program to get the object with the max attribute value in a list of objects using Python. This task can be achieved by using the max() method and attrgetter operator. It returns a callable object that fetches attr from its operand. If more than
3 min read
Python - Maximum Value in Nested Dictionary
Sometimes, while working with python dictionary, we can have problem in which each key in itself is a record with several keys and we desire to substitute the value as maximum value of keys of dictionary. This kind of problem can have application in many domains that involves data. Lets discuss cert
4 min read
Python - Maximum value assignment in Nested Dictionary
Sometimes, while working with Python dictionaries, we can have a problem in which we need to assign to the outer key, the item with maximum value in inner keys. This kind of problem can occur in day-day programming and web development domains. Let's discuss a way in which this task can be performed.
3 min read
Python - Extract Item with Maximum Tuple Value
Sometimes, while working with Python dictionaries, we can have a problem in which we need to extract the item with maximum value of value tuple index. This kind of problem can have application in domains such as web development. Let's discuss certain ways in which this task can be performed. Input :
5 min read
Min and Max value in list of tuples-Python
The task of finding the minimum and maximum values in a list of tuples in Python involves identifying the smallest and largest elements from each position (column) within the tuples. For example, given [(2, 3), (4, 7), (8, 11), (3, 6)], the first elements (2, 4, 8, 3) have a minimum of 2 and a maxim
3 min read
Python - Maximum value in record list as tuple attribute
Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the max of a list as a tuple attribute. Letâs discuss certai
8 min read
Python program to find Maximum value from dictionary whose key is present in the list
Given a list with dictionary keys and a dictionary, extract maximum from dictionary values, whose key is present in list. Examples: Input : test_dict = {"Gfg": 4, "is" : 5, "best" : 10, "for" : 11, "geeks" : 3}, test_list = ["Gfg", "best", "geeks"] Output : 10 Explanation : Max value is 11, but not
6 min read