AppDividend
Latest Code Tutorials

How to reverse a range in Python

0

Python’s inbuilt range() function is handy when you need to act a specific number of times. The range() function enables us to make a series of numbers within the given range. Depending on how many arguments you pass to the range() function, you can choose where that sequence of numbers will begin and end as well as how big the difference will be between one number and the next.

Syntax

range(start, stop, step)

Parameters

  1. start argument is the starting number of the series. i.e., lower limit. By default, it starts with 0 if not specified.
  2. stop argument is an upper limit. i.e., create numbers up to this number, The range() doesn’t include this number in the result.
  3. The step is the difference between each number in the output. The default value of the step is 1 if not specified.

Example of range()

for i in range(4, 24, 4):
    quotient = i / 4
    print(f"{i} divided by 4 is {int(quotient)}.")

Run Online

In this for loop, you were able to simply create a range of integers that are divisible by 4, so you didn’t have to provide each of them yourself.

Output

4 divided by 4 is 1.
8 divided by 4 is 2.
12 divided by 4 is 3.
16 divided by 4 is 4.
20 divided by 4 is 5

Incrementing With range() Function

If you have to increment, then you want a step to be a positive number. T be more descriptive of what this means in real-life, see the following code.

for i in range(3, 24, 10):
    print(f"{i}")

Run Online

Output

3
13
23

You got the range of numbers that were each greater than the preceding number by 10, which is the step you provided.

Now that you’ve seen how you can move forwards through a positive step in the range, let’s see how you can step backward using a negative step.

Python range reverse

Reversing a range or a sequence of numbers results in the sequence containing the numbers from the range in reverse order. If your step argument is negative, then you move through a sequence of decreasing numbers and are decrementing. This enables us to go over the numbers backward.

for i in range(5, 0, -1):
    print(f"{i}")

Run Online

Output

5
4
3
2
1

In the above example, our step is -1. That means that you’ll be decremented by 1 for each loop.

To reverse a range(5, 0, -1), your steps must be the following.

  1. Your start must be the maximum number of your range.
  2. Your stop must be a minimum number – 1 in your range.
  3. You have to decrement by 1(-1) to get the exact reverse of your list.

In the above code, we need to reverse 1 to 5, which means 5 to1. So, our maximum value starts, which is 5, then the minimum value -1 means 1-1 is 0, which is stop value, and we did not include that in the range. Then our step is -1. That means, 5, 5-1 = 4, 5-2=3, 5-3=2, 5-4=1. So, we get the exact reverse value.

Now, this is not the standard way, but if you want to reverse any kind of range(), then you have to calculate all the steps precisely. Not every time, this will be right. Let’s see a standard way to reverse range items.

Pythonic way to reverse range()

The most Pythonic way to generate a range that decrements is to use range(start, stop, step). But Python provides us with an inbuilt reversed() function. If you wrap range() method inside reversed() method, then you can print the integers in reverse order.

for i in reversed(range(5)):
    print(i)

Run Online

Output

5
4
3
2
1
0

The range() makes it easy to iterate through a decrementing series of numbers, whereas reversed() is used to loop over the series in reverse order.

Conclusion

To reverse range() in Python, we can use reverse() with range() function. Also, if you know the exact difference between the numbers then you can reverse the range by providing the negative step. That is it for the Python reverse range.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.