 # How can the range() function return a list of numbers incremented other than 1?

### Question

By default, the `range()` function returns a list of numbers incremented by 1. Is it possible to have the numbers increment by another value?

The `range()` function can be provided with three parameters. The first parameter is the starting number for the list. The second is the end value for the list (the list will stop before this number). The final value is the value used for the increment. By default, the value is 1 but it can be changed to any number greater than 0.

The following examples show two ranges - one where the numbers are incremented by 2 and the other incremented by 5.

``````by_two = range(1, 20, 2)
by_five = range(1, 20, 5)
``````
9 Likes

What @ajaxninja66418 said was correct, but you can also use negative values as parameters to the range().

But, you CAN use negative values or zero(0) in first two parameters of range() function.

``````increment_by_two = range(1, 40, 2)
decrement_by_two = range(40, 1, -2)
``````

Beg to differ…

``````>>> range(-1, -40, -3)
[-1, -4, -7, -10, -13, -16, -19, -22, -25, -28, -31, -34, -37]
>>>
``````

Edit

Python 3

``````>>> list(range(-1, -40, -3))
[-1, -4, -7, -10, -13, -16, -19, -22, -25, -28, -31, -34, -37]
>>>
``````
6 Likes

@mtf
I am sorry that I mentioned to not to use negative values in parameters of range() function.

I meant to not to pass single negative value as argument to the range() function.

``````#Not using negative values here
range(-1)
``````

I am sorry for that wrong part of information and I corrected it.

1 Like

The above will give us an empty range (`[]`). There is nothing syntactically incorrect so it won’t raise an error, although it won’t make much sense.

``````range(start_value, end_value, stride)
``````

When one of the index values is missing, the range function will not recognize the stride. Also, the end_value is exclusive, meaning not included in generated range.

4 Likes

Hey,
just fyi - range() in Python 2+ created a list object
xrange() - was just pure generator, which corresponds to range() function in Python3+
So just to finalize - range() in Python3 is a generator function which just reserves the instruction of how to pull integer links into object. You can iterate through range() to put values into list,array,dictionary,tuple,set u name it.
P.S. I am pretty sure my info is valid, but I study programming in General and Python for only 6 weeks so far, so I wouldn’t mind corrections or advices