How to reverse a range in python | Reverse Range
📋 Table Of Content
In this article, we will see different ways to reverse a range in python with some examples.
In Python, we use the range function often while working with loops, lists, and numbers. However, sometimes we might come across different circumstances where we might want to reverse the order of the sequence of numbers in a range.
Here, we will see different ways to achieve this task.
What is range in Python?
range() is an in-built function in python that returns an ordered sequence of numbers within a specified range. By default, the range starts with zero and increments by 1 till the specified stop number.
The start is the starting number of the range. By default, it is 0 (zero). It's the lower limit of the range.
The stop is the number upto which we want the range to stop .ie the upper limit. In range() the last number is excluded.
The steps is an optional argument. It is used to specify the incrementation in the range.
Examples of Python Range Function.
Let's print out the numbers between the range 0 to 5 using
for loop and
for i in range(5): print(i)
0 1 2 3 4
Here, we have passed only the stop number i.e 5. And since the default of the start is 0, the range is from 0 to 5.
We used the
for loop to iterate through the number and print it out on the screen.
Let's see what is the use of step argument in range.
Example with all three arguments ie (start, stop and step)
for i in range(2,10,2): print(i)
2 4 6 8
Here, we have 2 as start argument (lower limit) , 10 as stop argument (upper limit) and the step is set to 2.
Since the step is 2, the number was incremented by 2 ie. first is 2 then 2+2=4, 4+2 =6, and so on upto 10.
Note: In range() the stop number is not included so it output is till 8.
Now, since we have some basic understanding of the range function, let's see different ways to reverse it.
Reverse Python Range
The different ways we can use to reverse a range in python :
- Use the reversed() function with range.
- Using the negative step argument in range.
- Using the sorted() function on the range of numbers.
Let's check out each method with examples.
Using reversed() function in python
The easy and simple way to reverse a range in python is to use the reversed() function.
The reversed() is an in-built function that returns a list after it has reversed a sequence object in python.
We just have to pass the
range() function as a parameter in the
reversed() function to reverse the range in python.
for i in reversed(range(0,5)): print(i)
4 3 2 1 0
range function creates a sequence of number from 0 to 4 and the
reversed() function reverse the sequence and print it out on the screen.
Reverse a range using the negative step
We can also use a negative step in the
range() function to create a range in reverse.
We have to call the
range() function and pass on a negative number as the step argument to create a range in decrementing order.
for i in range(5, 0, -1): print(i)
5 4 3 2 1
Here, as you can see we have passed 5 as the start argument as we want to start from 5 and move upto (not including) 0 in decreasing order. So it's from 5 to 0.
Next, we have passed -1 as the step argument which means the number will be decremented by 1 (-1) .
So the range will start from 5 and then, 5-1=4, 4-1=3 and so on upto 1.
And thus we have created a reverse of the range 0 to 5 using the negative step argument.
Reverse a range using sorted() in python
sorted() function is an in-built python function that returns a sorted list from the specified iterable object.
It is used to sort items in ascending or descending order from iterable objects.
However, we can use the sorted function to reserve a range with the parameter
reverse_val = sorted(range(0,5), reverse=True) print(reverse_val)
[4, 3, 2, 1, 0]
Here, in this example, the
range(0,5) create a sequence of numbers from 0 to 5 like this [0, 1, 2, 3, 4] and then
sorted() function with the
reverse=true parameter reverse the whole number list to [4, 3, 2, 1, 0].
Here, we have learned three different ways to print numbers in reverse order in a range in python i.e using the reversed function, negative step, and the sorted function.
Other Articles You'll Also Like: