Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve largest negative number and smallest positive number from list [closed]

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?

like image 658
user3191569 Avatar asked Feb 11 '23 04:02

user3191569


1 Answers

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
like image 81
Two-Bit Alchemist Avatar answered Feb 16 '23 02:02

Two-Bit Alchemist