Examples of how to find the smallest positive value in a list in python ?
Let's consider the following list
l = [ 7, 3, 6, 9, 2, -1, -5, -4, -3]
to find the smallest value, a solution is to use the function min():
which returns here:
Now to find the smallest positive value a solution is to use a list comprehension and then min():
min([i for i in l if i > 0])
To find the index of the smallest positive value, a solution is to use index():
l.index(min([i for i in l if i > 0]))
Hi, I am Ben.
I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!