How to Reverse a Range in Python?

In Python, a range is a sequence of specific numbers that are generated using the “range()” function. Sometimes, users may need to reverse a particular range in Python for different reasons, such as to count down numbers from highest to lowest or loop through a list from the last element to the first. Reversing the order of a range is an ordinary task for which Python provides various built-in functions. This blog will exemplify the different methods to reverse a range in Python. 

Quick Outline

  1. How to Create/Generate Range in Python?
  2. How to Reverse Specific Range in Python?
  3. How to Create and Reverse a Float Range in Python?

How to Create/Generate Range in Python?

In order to create the range in Python, the “range()” function is utilized. The “range()” function is Python’s built-in function that generates/returns a series of numbers within the specified range.

Syntax

The basic syntax of the “range()” is:

range(start, stop, step)

Arguments 

The “range()” function can accept up to three arguments. These arguments include start, stop, and step. 

  • Start is an optional argument. It is used to define the start/initial value in the range or of the sequence. By default, this value is 0.
  • Stop is an essential argument that must be defined. It is used to define the end value of the range or sequence and is not included in the sequence. 
  • Step is also an optional argument. It is utilized to define the incrementation. Its default value is 1.

Check out the provided examples to see how to create a range using different arguments. 

Example 1: Using “range()” Function With the Stop Argument

In this example, we will create a range by passing only one (stop) argument i.e. “5” to the “range()” function. This will generate a series of numbers from 0 to 4:

range1 = range(5)

print(list(range1))

Here: 

  • range(5)” function creates a sequence of numbers from 0 to 4. 
  • range1” is the variable that stores the created range.
  • list(range1)” function converts the range object into a number’s list. 
  • print()” function prints the range.

This has created the specified range/sequence:

Example 2: Using the “range()” Function With Start and Stop Argument

We will define the start and stop arguments to the “range()” function to create a range. The start augment is “1” and the stop argument is “6”. This indicates that the sequence of numbers should start from “1” and stop at “5”: 

range2 = range(1, 6)

print(list(range2))

This has created a range from “1” to “5”:

Example 3: Using the “range()” Function With Start, Stop, and Step Argument

Here, we will create a range by utilizing all three arguments in the “range()” function. We have defined the “start” argument as “1”, the “stop” argument as “10”, and the “step” argument as “2”. This means that the range should be created from “1” and end at “9” with the step value or increment of “2”:

range3 = range(1, 10, 2)

print(list(range3))

This has generated the following range:

Moreover, users can also use another straightforward way to create a range i.e. using the “range()” function with the “for” loop. Here is the way to do so:

for i in range(1, 10, 2):
    print(i)

This has created a range from “1” to “9” with the step value “2”: 

How to Reverse Specific Range in Python?

Reversing a range means changing the order of the specific sequence in the reverse (opposite) direction. There are different methods in Python to reverse a particular range, such as:

  • Method 1: Reverse a Range Through “reversed()” Function
  • Method 2: Reverse a Range Through “range()” Function With a Negative Step
  • Method 3: Reverse a Range Through “sorted()” Function
  • Method 4: Reverse a Range Through List Slicing

Method 1: Reverse a Range Through “reversed()” Function

The “reversed()” function is Python’s built-in function that is commonly utilized to reverse the iterable object. It accepts an iterable object as its parameter, converts it to a reversed range, and returns it in reversed order. 

To reverse a range using the “reversed()” function, users must convert it to an iterable object or a list and then apply the “reversed()” function to the object. Follow the provided instructions to do so.

First, define the range object with the help of the “range()” function. For instance, we are creating a range from 10 to 15 and storing it in a “myRange” variable:

myRange = range(10, 16)

Then, use the “list()” function to convert/transform the defined range object into a list. Here, we are converting the “myRange” range object to a list and displaying it:

lst = list(myRange)

print(lst)

In the below output, the specified range can be seen as a list:

Now, reverse the list (range) using the “reversed()” function and use the “list()” function again to convert the reversed iterator back to a list. Finally, print the reversed range:

rev_range = list(reversed(lst))

print(rev_range)

The below output shows that the given range has been reversed successfully:

Furthermore, users can also print each number in a separate line as seen below:

for i in rev_range:
  print(i)

Alternative Way

There is an easiest alternative way to reverse the range using the “reversed()” function in Python. Users can use the “for” loop and directly pass the “range()” function as a parameter in the “reversed()” function. Here, we are reversing the range from 10 to 15 as seen below:

for i in reversed(range(10, 16)):
    print(i)

This has reversed the specified range successfully:

Method 2: Reverse a Range Through “range()” Function With a Negative Step

To reverse a range in Python, the negative steps value can be used in the “range()” function. Users can create a range in reverse order by utilizing the “range()” function and passing a negative value (number) to it as the step argument. The start value must be the range’s maximum number and the stop value should be a minimum number of “-1” in the range. Go through the following instructions for a better understanding. 

First, define the range object through the “range()” function. Then, use the “list()” function to print it in the list form. For instance, we are creating a range from 1 to 9, storing it in a “my_range” variable, and printing it:

my_range = range(1, 10)

print(list(my_range))

The below output displays the created range:

Next, utilize the “range()” function again and specify the maximum, and minimum numbers in the above-created range and negative step value. Here, we have specified the following arguments: 

rev_range = range(max(my_range), min(my_range) -1, -2)

res = list(rev_range)

print(res)

Here:

  • max(my_range)” sets the default value as a maximum number in the “my_range” range.
  • min(my_range) -1” sets “-1” as the minimum number in the “my_range” range to ensure that it should be generated in reverse order.
  • -2” is the negative step value that indicates that each number in the range should be decreased by “2” every time. 
  • rev_range” is the variable that stores the reverse generated range.
  • list(rev_range)” function converts the reversed range to a list.
  • print()” function prints the reversed range in the form of a list.

According to the below output, the range has been reversed successfully:

Alternative Way

Users can also use the “for” loop and pass the start, stop, and negative step values to the “range()” function in one step to generate the reversed range. Here, we are reversing the range from 10 to 0 with the step size “2” as seen below:

for n in range(10, 0, -2):
    print(n)

This has generated the reversed range:

Method 3: Reverse a Range Through “sorted()” Function

The “sorted()” function is also Python’s built-in function that generates and returns a sorted list from the given iterable object. It can be used to generate the range in the reverse order with the help of the “reverse=True” parameter. Follow the given steps to understand the workings of this function. 

First, define the range object and print it in the form of a list. Here, we are creating a range from 20 to 25:

myRange = range(20, 26)

print(list(myRange))

This has created the specified range:

Then, use the “sort()” function and pass the range object and the “reverse=True” parameter to it as seen below:

revRange = sorted(myRange, reverse=True)

print(revRange)

By doing so, the range has been generated in the reverse order: 

Alternative Way

Alternatively, use the “for” loop and directly pass the “range()” function and “reverse=True” parameter in the “sorted()” function as seen below:

for i in sorted(range(20, 26), reverse=True):
    print(i)

It can be observed that the range has been reversed successfully:

Method 4: Reverse a Range Through List Slicing

To reverse a desired range in Python, the list-slicing method is also utilized. Users are required to use the “list()” function and apply list-slicing with the “[::-1]” syntax. To do so, look at the provided instructions. 

First, define the range object and print it in the form of a list. For instance, we are creating a range from 5 to 9:

my_range = range(5, 10)

print(list(my_range))

This has created the specified range:

Now, use the “list()” function to convert the above-created range to a list and apply the list slicing to reverse the range element’s order. Here, we are using the list-slicing i.e. “[::-1]” to make a slice that begins from the list’s end and goes backward:

rev_range = list(my_range)[::-1]

print(list(rev_range))

As you can see, the range has been reversed successfully:

Alternative Way

Users can also utilize the “for” loop with the “range()” function and “[::-1]” syntax in one step to generate the reversed range. Here, we are reversing the range from 5 to 10 with the step size “1” as seen below:

for i in range(5, 10)[::-1]:
    print(i)

The below output displays the reversed range:

How to Create and Reverse a Float Range in Python?

Python’s “range()” function only works with integer values and does not work with decimal or floating-point values. If the user passes any floating-point value to the “range()” function as an argument, it shows an error as seen below:

So, to create a range with floating-point values, the “arrange()” function of the “NumPy” library is used. Users can pass floating-point numbers to the “arrange()” function as the arguments. Go through the following example for a practical demonstration. 

First, import the “numpy” module. Then, use the “np.arrange()” function and pass the decimal numbers as arguments to create a range of floating points. Finally, use the “for” loop to print each element of the range in a separate line: 

import numpy as np

new_range = np.arange(1.5, 5.5)

for i in new_range:
    print(i)

This has successfully created a range of floating points numbers (float range):

To reverse this float range, simply use the “reversed()” function and pass the “np.arrange()” function as a parameter:

new_range = reversed(np.arange(1.5, 5.5))

This has generated the float range in the reverse order:

Note: Click on the provided link to access Google Colab. 

Final Thoughts

To reverse a range in Python, different methods can be used. Users can use various built-in functions, such as the “reversed()” function, “range()” function with a negative step, “sorted()” function, or the list slicing method to reverse a range. Moreover, all these functions can also be used with the “for” loop to easily generate the range in reverse order. This blog has efficiently explained all the methods to create and reverse a range in Python.