Given a list of integers, e.g.:
lst = [-5, -1, -13, -11, 4, 8, 16, 32]
is there a Pythonic way of retrieving the largest negative number in the list (e.g. -1
) and the smallest positive number (e.g. 4
) in the list?
You can just use list comprehensions:
>>> some_list = [-5, -1, -13, -11, 4, 8, 16, 32]
>>> max([n for n in some_list if n<0])
-1
>>> min([n for n in some_list if n>0])
4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With