I am looking to find the lowest positive value in an array and its position in the list. If a value within the list is duplicated, only the FIRST instance is of interest. This i
You can use the min
function and enumerate
function, like this
result = min(enumerate(a), key=lambda x: x[1] if x[1] > 0 else float('inf'))
print("Position : {}, Value : {}".format(*result)
# Position : 3, Value : 1
This makes sure that, if the value is greater than 0
, then use that value for the minimum value comparison otherwise use the maximum possible value (float('inf')
).
Since we iterate along with the actual index of the items, we don't have to find the actual index with another loop.