What is the efficient way to find the previous and next elements based on a given element index?
list = [(100, 112), (100, 100), (200, 100), (200, 125), (240, 130), (240, 100), (272, 100)]
idx = 2 # for example
print('Next elements are', list[idx + 1:] )
Correct output
Next elements are [(200, 125), (240, 130), (240, 100), (272, 100)]
while this line prints wrong elements:
print ('Prev elements are', list[idx - 1:])
Wrong output
[(100, 100), (200, 100), (200, 125), (240, 130), (240, 100), (272, 100)]
Why this is happening? I tried this too list[(idx-1)%len(list):]