Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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
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
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]
Advertisements
