How to reverse a range in python | Reverse Range

featured Image

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?

A 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.

Syntax:

range(start,stop,steps)

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 range function.

Example:

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

Output:

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)

Output:

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 :

  1. Use the reversed() function with range.
  2. Using the negative step argument in range.
  3. 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)

Output:

4
3
2
1
0

The 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.

Example:

for i in range(5, 0, -1):
    print(i)

Output:

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

The 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=true.

Example:

reverse_val = sorted(range(0,5), reverse=True)
print(reverse_val)

Output:

[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].

Conclusion:

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:

Sort by two keys in Python

Related Posts

merge json files in python

How to merge JSON files in python

JSON (JavaScript Object Notation) is a data format used for storing, transferring, and visualizing data. Its simple structure of key:value pairs and array data types makes JSON an easy and…

Read more
Install python pip in ubuntu

How to Install Pip(pip3) in Ubuntu Linux 22.04

Pip is a package manager for Python that allows you to install and manage additional Python packages that are not part of the Python standard library. Pip makes it easy…

Read more
featured Image

How to Write If-Else Statements in One Line in Python

If-else statements are a fundamental concept in Python and other programming languages. They allow you to execute different blocks of code based on whether a condition is true or false….

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *