Kodeclik Blog

How to reverse a range in Python

The range() operator is very common in Python and you will usually see it in for loops, like so:
The output is:
The range operator essentially says we begin the variable ā€œiā€ indexed at 1, and we keep incrementing it (by 1) until we exceed 10. To make the increment size explicit, we can do:
The output is the same as before:
We can make the stepsize to be more than 1, such as 2:
The output will be:
As can be seen, the range operator above takes three arguments. It starts at 1 (the first argument) and increments it by 2 (the third argument). It stops when it exceeds 10 (the second argument). In fact, the range operator really returns a list, so you could try to do:
This yields:
which is not quite informative. To see the elements in the range, you should do:
This will give the output we are looking for:
In Python, therefore, the range() function is a helpful tool for producing numerical sequences. It's frequently used in for loops as we show above, to iterate over a set number of items. However, sometimes you may need to reverse the order of the sequence. This blogpost shows you two ways to achieve this purpose.

Reversing a range using a negative step size

The easiest way to count down rather than count up is to use a negative step, like so:
The output will be:
Note that the range will stop before it reaches 1 (i.e., the second argument is considered to be outside the range).
Let us try a negative step size but higher, like:
The output will be:
as expected.

Reversing a range using the reversed() function

A different way to reverse a range is to write the range operation as if it is incrementing and then use the reversed() function:
Here, the range goes from 3 in steps of 3, so it yields values of 3, then 6, then 9. The reversed() function flips the order, so the above statement prints:
This is thus another way to reverse a range. But note that the actual results returned by the reversed() function will not be quite the same as if you had simply flipped the start and ending values and negated the step size.
So if we do:
The output is:
This is because in the first example we started at 3 (before reversing) and we will never reach 10 going in steps of 3. In the second example, we started at 10 and we will never reach 3 going in steps of -3. You have to take this into account when you use reversed() as an option for reversing a range.
For more Python content, checkout the math.ceil() and math.floor() functions! Also learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.

Copyright @ Kodeclik 2022. All rights reserved.