Highest value that is less than 0 in a list that has a combination of negative and positive values

I have a list with these values.

lst1 = [1,-2,-4,-8,-9,-12,0,39,12,-3,-7] 

I need to get the maximum value that is less than zero.

If I do print max(last) - I get 39, and the need is -2.

print max(p < 0 for p in lst1) , I get True, not -2

+6
source share
2 answers

Nothing, I understood, and it should be

 print max(p for p in lst1 if p < 0) 
+12
source

just filter the list first:

 max(filter(lambda x:x<0,ls)) 
+3
source

All Articles