Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For loop range and interval, how to include last step

What is the best way to make a for loop such that

 for i in range(0,99,20)

iterates over i = 0 20 40 60 80 99?

rather than just i = 0 20 40 60 80?

Right now I have an if statement to check if modulo != 0 and tacks on an extra iteration if true.

like image 929
aolxwzubx Avatar asked Sep 11 '25 15:09

aolxwzubx


2 Answers

This is impossible to do with Python's range. But this can be accomplished by creating your own generator function.

def myRange(start,end,step):
    i = start
    while i < end:
        yield i
        i += step
    yield end


for i in myRange(0,99,20):
    print(i)

Output:

0
20
40
60
80
99
like image 121
Neil Avatar answered Sep 14 '25 03:09

Neil


First of all, it usually does not makes much sense to include the end condition, the idea of a range is to perform hops until the end value is reached.

Nevertheless, you can for instance use itertools.chain for that:

from itertools import chain

for i in chain(range(0,99,20), [99]):
    # ...
    pass

chain concatenates iterables together. So after the range(..) is exhausted, it will iterate over the next iterable, and so on.

The above approach is not very elegant: it requires some knowledge about how chain works. We can however encapsulate that logic:

def range_with_end(start, stop, step):
    return chain(range(start, stop, step), (stop,))
like image 21
Willem Van Onsem Avatar answered Sep 14 '25 05:09

Willem Van Onsem