Say I have a list,
l = [1, 2, 3, 4, 5, 6, 7, 8]
I want to grab the index of an arbitrary element and the values of its neighbors. For examp
You could use the modulo operator!
i = len(l) - 1 jIndex = (i - 1) % len(l) kIndex = (i + 1) % len(l) j = l[jIndex] k = l[kIndex]
Or, to be less verbose:
k = l[(i + 1) % len(l)]