How to reverse a Range in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Reverse a range in Python #

Use the reversed() function to reverse a range, e.g. reversed_range = reversed(range(1, 11)). The reversed function takes an iterator, such as a range, reverses it and returns the result.

main.py
# ✅ reverse a range using reversed() my_range = reversed(range(1, 11)) my_list = list(my_range) print(my_list) # 👉️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # --------------------------------------------------- # ✅ reverse a range using a negative step my_range = range(10, 0, -1) my_list = list(my_range) print(my_list) # 👉️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # --------------------------------------------------- # ✅ loop through a range of numbers backwards for i in range(10, -1, -1): print(i) # 👉️ 10 9 8 7 6 5 ... 2 1 0 for i in reversed(range(1, 11)): print(i) # 👉️ 10 9 8 7 6 ... 3 2 1

The first example uses the reversed() function to reverse a range object.

The reversed function takes an iterator, reverses it and returns the result.

main.py
my_range = reversed(range(1, 11)) my_list = list(my_range) print(my_list) # 👉️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] my_range = reversed(range(1, 6)) my_list = list(my_range) print(my_list) # 👉️ [5, 4, 3, 2, 1]

If you need to view the numbers in the range, use the list class to convert it to a list.

The list class takes an iterable and returns a list object.

Alternatively, you can use a negative step.

Reverse a range using a negative step #

Use a negative step to reverse a range, e.g. reversed_range = range(10, 0, -1). The range() class can be passed a step of -1 to create a range object that counts backwards from a specified number.

main.py
my_range = range(10, 0, -1) my_list = list(my_range) print(my_list) # 👉️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

We used a negative step to reverse a range.

The range class is commonly used for looping a specific number of times in for loops and takes the following arguments:

NameDescription
startAn integer representing the start of the range (defaults to 0)
stopGo up to, but not including the provided integer
stepRange will consist of every N numbers from start to stop (defaults to 1)

We used 10 for the start value and 0 for the stop value.

Notice that the start value is inclusive and the stop value is exclusive (up to, but not including).

We specified a step of -1 to start counting backwards from the start value.

Iterate over a range of numbers in reverse order #

To iterate over a range of numbers in reverse order:

  1. Use the reversed() function to reverse the range.
  2. Use a for loop to iterate over the reversed range.
main.py
for i in reversed(range(1, 6)): print(i) # 👉️ 5 4 3 2 1

We used the reversed() function to reverse the range.

Note that the start value is inclusive and the stop value is exclusive (up to, but not including).

You could also use a negative step to iterate over a range in reverse order.

main.py
for i in range(5, 0, -1): print(i) # 👉️ 5 4 3 2 1

The start value in the range is 5, the stop value is 0 and the step is -1.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.