If you want to over-complicate things, you could create a custom generator where you can use the generator.send
method to pass in a new step during iteration.
def variable_step_generator(start, stop=None, step=1):
if stop is None:
start, stop = 0, start
while start < stop:
test_step = yield start
if test_step is not None:
step = test_step
yield
start += step
With usage like:
variable_step_range = variable_step_generator(1, 100)
for i in variable_step_range:
print i
if i == 10:
variable_step_range.send(10)
if i == 90:
variable_step_range.send(1)
# 1, 2, 3, 4, 5, 6, 7, 8, 9,
# 10, 20, 30, 40, 50, 60, 70, 80, 90,
# 91, 92, 93, 94, 95, 96, 97, 98, 99
But this isn't really much more than a wrapper around the while
loop that the other answers suggest.