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 example,
i = l.index(n)
j = l[i-1]
k = l[i+1]
However, for the edge case when i == len(l) - 1
this fails. So I thought I'd just wrap it around,
if i == len(l) - 1:
k = l[0]
else:
k = l[i+1]
Is there a pythonic way to do this?
j
andk
to point to something. And I want to be able to traverse the entire list viaj
ork
.