I have a list e.g. my_list = [1, 3, 5, 7, 14, 16, 18, 22, 28, 30, 32, 41, 43]
I want a function that will return all values from the list where the difference between that value and previous value is not equal to 2, e.g. the function will return [1, 14, 22, 28, 41]
for the above list. Note that the first value of my_list
will always appear as the first value of the output. The input lists are of non-zero length and up to the order of 100's.
So far I have this:
def get_output(array):
start = [array[0]]
for i in range(1, len(array)-1):
if (array[i] - array[i-1]) != 2:
start.append(array[i])
return start
Is there a vectorised solution that would be faster, bearing in mind I will be applying this function to thousands of input arrays?