Find maximum value in each sublist in Python


We are given a list of lists. In the inner lists or sublists we are required to find the maximum value in each.

With max and in

We design a for loop with in condition and apply the max function to get the maximum value in each of the sublist.

Example

 Live Demo

Alist = [[10, 13, 454, 66, 44], [10, 8, 7, 23]]
# Given list
print("The given list:\n ",Alist)
# Use Max
res = [max(elem) for elem in Alist]
# Printing max
print("Maximum values from each element in the list:\n ",res)

Output

Running the above code gives us the following result −

The given list:
[[10, 13, 454, 66, 44], [10, 8, 7, 23]]
Maximum values from each element in the list:
[454, 23]

With map and max

We keep applying the max function using map while iterating through the sublists.

Example

 Live Demo

Alist = [[10, 13, 454, 66, 44], [10, 8, 7, 23]]
# Given list
print("The given list:\n ",Alist)
# Use Max
res =list(map(max, Alist))
# Printing max
print("Maximum values from each element in the list:\n ",res)

Output

Running the above code gives us the following result −

The given list:
[[10, 13, 454, 66, 44], [10, 8, 7, 23]]
Maximum values from each element in the list:
[454, 23]

Updated on: 04-Jun-2020

269 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements