How to Reverse a Range in Python

To create a reverse range in Python pass a negative value as the third argument of the range() function. This value is the number of steps to take between each number in a range.

 

To demonstrate this, let's create a list of numbers from 0-5 in descending order.

 

numbers = range(5, 0, -1)

for n in numbers:
   print(n)
5
4
3
2
1

 

Reverse an Existing Python Range

To reverse an existing range, use the Python reversed() function. Store the result in a new variable as it will not modify the original range.

 

numbers = range(1, 5)

numbers_desc = reversed(numbers)

for n in numbers_desc:
   print(n)
4
3
2
1
numbers