Get maximum number in each sublist using reduce() method
Here is another approach using the reduce function from the functools module and the max function:
Python3
from functools import reduce # Initialising List Input = [[ 10 , 13 , 454 , 66 , 44 ], [ 10 , 8 , 7 , 23 ]] #Find max in each list Output = [ reduce ( max , sublist) for sublist in Input ] print (Output) #This code is contributed by Edula Vinay Kumar Reddy |
[454, 23]
This approach uses a list comprehension to iterate through each sublist in the Input list. The reduce function is used to apply the max function to the elements of the sublist and reduce the sublist to a single value, which is the maximum value.
This approach has a time complexity of O(n), where n is the number of elements in the input list, as the list comprehension iterates through each sublist and the reduce function iterates through each element in the sublist. The space complexity is also O(n), as the output list will have a maximum size of n if every sublist in the input list contains at least one element.
Python | Find maximum value in each sublist
Given a list of lists in Python, write a Python program to find the maximum value of the list for each sub-list.
Examples:
Input : [[10, 13, 454, 66, 44], [10, 8, 7, 23]] Output : [454, 23] Input : [[15, 12, 27, 1, 33], [101, 58, 77, 23]] Output : [33, 101]