Print a range in reverse order in Python

avatar

Borislav Hadzhiev

Last updated: Aug 29, 2022

banner

Photo from Unsplash

Print a range in reverse order in Python #

To print a range in reverse order:

  1. Use the reversed() function to reverse the range.
  2. Use the list() class to convert the range to a list.
  3. Use the print() function to print the reversed range.
main.py
# ✅ print range in reverse order using reversed() result = list(reversed(range(1, 11))) print(result) # 👉️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # ------------------------------------ # ✅ print range in reverse order using negative step result = list(range(10, 0, -1)) print(result) # 👉️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] # ------------------------------------ # ✅ iterate through a range of numbers backwards for i in range(10, -1, -1): print(i) # 👉️ 10 9 8 7 6 5 ... 2 1 0

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
result = list(reversed(range(1, 11))) print(result) # 👉️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

To be able to print the contents of the range() object, 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.

Use a negative step to print a range in reverse order, e.g. print(list(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
result = list(range(10, 0, -1)) print(result) # 👉️ [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

We used a negative step to print a range in reverse order.

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.

You can use the same approach to iterate through a range of numbers in reverse order.

main.py
for i in range(10, -1, -1): print(i) # 👉️ 10 9 8 7 6 5 ... 2 1 0

The for loop prints each number in the range on a separate line.

If you need to print the numbers in the range separated by a space, set the end argument to a space.

main.py
for i in range(10, -1, -1): print(i, end=' ') # 👉️ 10 9 8 7 6 5 ... 2 1 0

The end argument is printed at the end of the message.

By default, end is set to a newline character (\n).

By setting the argument to a space, we print the number in the range on the same line, separated by spaces.

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.