Open In App

Python | Find maximum value in each sublist

Improve
Improve
Like Article
Like
Save
Share
Report

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]

Get maximum value in each sublist using loop

Here, we are selecting each list using a Python loop and find a max element in it, and then appending it to our new Python list.

Python3




# Initialising List
a = [[10, 13, 454, 66, 44], [10, 8, 7, 23]]
lis = []
 
# find max in list
for p in a:
    lis.append(max(p))
 
# Printing max
print(lis)


Output:

[454, 23]

Time complexity: O(n * m), where n is the number of sublists in a and m is the number of elements in the largest sublist.
Auxiliary space: O(n), where n is the number of sublists in a. This is because list is used to store the maximum element from each sublist.

Get maximum value in each sublist using list comprehension

Here, we are selecting each list using a Python list comprehension and finding a max element in it, and then storing it in a Python list.

Python3




# Initialising List
a = [[10, 13, 454, 66, 44], [10, 8, 7, 23]]
 
# find max in list
b = [max(p) for p in a]
 
# Printing max
print(b)


Output:

[454, 23]

Time complexity: O(n * m), where n is the number of sublists in a and m is the number of elements in the largest sublist.
Auxiliary space: O(n), where n is the number of sublists in a. This is because list is used to store the maximum element from each sublist.

Get maximum number in each sublist using a map 

Here we are using a map method to get the maximum element from each list using a Python map.

Python3




# Initialising List
a = [[10, 13, 454, 66, 44], [10, 8, 7, 23]]
 
# find max in list
ans = list(map(max, a))
 
# Printing max
print(ans)


Output:

[454, 23]

Time complexity: O(nm), where n is the number of sublists in the list a and m is the maximum length of any sublist.
Auxiliary space: O(n), where n is the number of sublists in the list a. 

Get maximum number in each sublist using sort() method

Python3




# Initialising List
a = [[10, 13, 454, 66, 44], [10, 8, 7, 23]]
lis = []
 
# find max in list
for p in a:
    p.sort()
    lis.append(p[-1])
 
# Printing max
print(lis)


Output

[454, 23]

Time Complexity: O(m * nlogn), where m is length of list ‘a’ and n is length of sub-list in list ‘a’.
Auxiliary Space: O(n), where n is length of list ‘lis’.

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


Output

[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.

Get maximum value in each sublist using a lambda function and the map() function:

First define a list of sublists a. Then, use the map() function to apply a lambda function to each sublist in a. The lambda function simply finds the maximum value in the sublist using the built-in max() function.

Finally, we convert the result of the map() function to a list using the list() function, and assign it to the variable lis. Finally, we print the resulting list of maximum values.

Python3




# define a list of sublists
a = [[10, 13, 454, 66, 44], [10, 8, 7, 23]]
 
# use the map function to apply a lambda function to each sublist in a
lis = list(map(lambda x: max(x), a))
 
# print the resulting list of maximum values
print(lis)


Output

[454, 23]

Time complexity: O(n*m)

  • The map() function applies the lambda function to each sublist in a. Since there are n sublists and finding the maximum value in a sublist takes O(m) time, where m is the length of the sublist, the overall time complexity is O(nm).

Auxiliary space: O(n)

  • The map() function returns a new list of the same length as a, which takes O(n) space. The list() function also creates a new list of the same length as a. The lambda function used in map() does not create any additional space. Therefore, the overall auxiliary space complexity is O(n).

Method 7: Get maximum value in each sublist using the built-in max() function inside a loop

Python3




# define a list of sublists
a = [[10, 13, 454, 66, 44], [10, 8, 7, 23]]
 
# create an empty list to store the maximum values
max_values = []
 
# loop through each sublist in a
for sublist in a:
    # use the max() function to get the maximum value in the current sublist
    max_value = max(sublist)
    # append the maximum value to the max_values list
    max_values.append(max_value)
 
# print the resulting list of maximum values
print(max_values)


Output

[454, 23]

Time complexity: O(n * m), where n is the number of sublists in the list and m is the length of the longest sublist.
Auxiliary space: O(n), where n is the number of sublists in the list. 



Last Updated : 05 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads