So I have a list
stock = [5,6,8,4,8,3,6,4]
val = 5
I want to get the index of the element closest to the 1st occurrence of the val variable in the list. So what I want to get will be 4 with index 3. if val = 8 then I will get 6 with index 1, if val = 3, since its the lowest value, I will get -1 I have tried using this code.
closest = min(range(len(stock)), key=lambda i: abs(stock[i]-val))
but it just returns back the index of val variable