li = [0, 1, 2, 3]
running = True
while running:
for elem in li:
thiselem = elem
nextelem = li[li.index(elem)+1]
When this reaches the last element, an IndexError
is raised (as is the case for any list, tuple, dictionary, or string that is iterated). I actually want at that point for nextelem
to equal li[0]
. My rather cumbersome solution to this was
while running:
for elem in li:
thiselem = elem
nextelem = li[li.index(elem)-len(li)+1] # negative index
Is there a better way of doing this?