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