Home > Net >  python list comprehension :find largest number in sublist using list comprehension
python list comprehension :find largest number in sublist using list comprehension

Time:03-11

I am trying to find the largest number in a sub list which present in list using list comprehension.

max_list = []
max_element = [ max_list:=j  for i in range(len(list1)) 
                             for j in list1[i] if j>max1 ]

I want output like [7,6,9] which is largest in each sublist.

This is my input

list1 = [[1,7,3],[4,5,6],[7,8,9]]

CodePudding user response:

There's the function max() to have the biggest number in a list. I would solve the problem as follow:

for list_num in list1:
        list_max.append(max(list_num))

CodePudding user response:

try this:

lst = [[1,7,3],[4,5,6],[7,8,9]]
print([max(item) for item in lst])
  • Related