if statement whilst slicing

Viewed 2494

I have a number of lists that I am slicing, such that (using example data):

midpoint = [[0.2], [0.5], [0.6]]
Values = [[0.1, 0.3, 0.6, 0.8], [0.2,0.3,0.5,0.7], [0.2,0.5,0.6,0.9]]
numbers = numpy.arange(0, len(Values), 1) 

I then have a loop that finds the index position from numbers of the first value that is greater or less than the midpoint value. I have:

for i in range(len(Values)):
    indexgtr = numbers[Values[i]>=midpoint[i]][-1] # The position of the first number larger than the midpoint
    indexlt = numbers[Values[i]<=midpoint[i]][0] # The position of the first number larger than the midpoint

I then use these index positions to return values from some other lists that I have.

The problem I have is that sometimes there is no value greater or less than the midpoint so indexgtr or indexlt return an empty list [] and I get the error IndexError: index 0 is out of bounds for axis 0 with size 0.

Is there something that I could add that would catch this and substitute a value of 0 into the empty list?

2 Answers
Related