47

Currently if I want to iterate 1 through n I would likely use the following method:

for _ in range(1, n+1):
    print(_)

Is there a cleaner way to accomplish this without having to reference n + 1 ?

It seems odd that if I want to iterate a range ordinally starting at 1, which is not uncommon, that I have to specify the increase by one twice:

  1. With the 1 at the start of the range.
  2. With the + 1 at the end of the range.
10
  • 3
    Why do you think this is a "dirty" way?
    – sobolevn
    Oct 22, 2015 at 13:45
  • 1
    n+1 evaluates once, so this is not a dirty way. As an option, if you don't like n+1 as the range parameter, evaluate it earlier and save the result to a variable, then use this variable in the range call.
    – user784540
    Oct 22, 2015 at 13:47
  • 1
    I have to specify the increase by one twice You're not doing this even once. You're specifying the start (1) and the end (n+1) of the range. You're not specifying an increase/increment at all Oct 22, 2015 at 13:54
  • I want to iterate n times, but have to modify the reference to n in order to establish the iteration.
    – cjhines
    Oct 22, 2015 at 13:58
  • 1
    The problem seems to be that n is improperly assigned. If you correct the assignment of n (wherever that is, elsewhere in your code) then you can just do for i in range(1,n): but you may need to n+=1 because a range is indexed from 0, so range(2) == [0,1], etc. Oct 22, 2015 at 13:59

5 Answers 5

38

From the documentation:

range([start], stop[, step])

The start defaults to 0, the step can be whatever you want, except 0 and stop is your upper bound, it is not the number of iterations. So declare n to be whatever your upper bound is correctly and you will not have to add 1 to it.

e.g.

>>> for i in range(1, 7, 1): print(i)
... 
1
2
3
4
5
6
>>> for i in range(1, 7, 2): print(i)
... 
1
3
5

A nice feature, is that it works in reverse as well.

>>> for i in range(7, 0, -1): print(i)
... 
7
6
5
4
3
2
1

If you aren't using it as an index but for something that can have positive or negative values, it still comes in handy:

>>> for i in range(2, -3, -1): print(i)
... 
2
1
0
-1
-2
>>> for i in range(-2, 3, 1): print(i)
... 
-2
-1
0
1
2
18

range(1, n+1) is not considered duplication, but I can see that this might become a hassle if you were going to change 1 to another number.

This removes the duplication using a generator:

for _ in (number+1 for number in range(5)):
    print(_)
2
  • 1
    Nice answer to the question but have we really moved forward from range(1, n+1) Oct 22, 2015 at 15:44
  • 5
    I wouldn't say so. I just had this cool generator idea which seemed really pythonic and wanted to share it. Oct 22, 2015 at 17:25
4

range(1, n+1) is common way to do it, but if you don't like it, you can create your function:

def numbers(first_number, last_number, step=1):
    return range(first_number, last_number+1, step)

for _ in numbers(1, 5):
    print(_)
3
for i in range(n):
    print(i+1)

This will output:

1 
2
...
n    
1
  • 2
    This doesn't mean much cleaner, as I would have to use +1 whenever I want to reference loop count. Rather than using +1 once when defining the loop I may have to use it multiple times.
    – cjhines
    Oct 22, 2015 at 13:44
3

Not a general answer, but for very small ranges (say, up to five), I find it much more readable to spell them out in a literal:

for _ in [1,2,3]:
    print _

That's true even if it does start from zero.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.