Cycle

Cycles through items in a list, rolls over.

l = [1, 2, 3, ...]

# access next item
l[(current_index + 1) % len(l)]

# access previous item
l[(current_index - 1 + len(l)) % len(l)]

The % (modulo) operator is not just useful for checking if a number is a multiple of something, it’s also useful for cycling/repeating things.

#programming