I'm looking for a way to iterate over a list oblist
with objects starting at a given index x
. From this index each iteration I would like to get the two neighboring element indices x-1
and x+1
. The iteration should not stop when one side reaches the end of the list, but the other side is not exhausted yet and still has elements. A None
element should be returned in this case for the exhausted side.
I tried a couple ways with
for element_indexprev, element_indexnext in zip(range(1,len(oblist)), range(-1,len(oblist), - 1)):
but I'm not getting the desired output. Probably also not the best approach.