Examples of how to find the smallest positive value in a list in python ?

Table of Contents

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():

`min(l)`

which returns here:

`-1`

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])`

returns

`2`

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]))`

returns here:

`4`

License

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.