How to Reverse a Range in Python?

4.5/5 - (2 votes)

Summary: Use negative step in the range() function to reverse the given sequnce as: range(start, stop, -step). Another workaround is to apply the reversed() function to the given range to reverse it as: reversed(range(start, stop)).

Problem: Given a range/sequence of numbers, how will you reverse the range?

Example:

Given:
# Given range
for i in range(1, 6):
print(i, end=” “)
# 1 2 3 4 5
Expected Output:
5 4 3 2 1

So, reversing the range simply means reversing the order of the given sequence.

Prerequisite:

The Python range() function creates an iterable of subsequent integers within a given range of values. You can pass either only a stop argument in which case the range object will include all integers from 0 to stop (excluded). Or you can pass startstop, and step arguments in which case the range object will go from start to step using the given step size. For example, range(3) results in 0, 1, 2 and range(2, 7, 2) results in 2, 4, 6.

Python range() Function | A Helpful Illustrated Guide

πŸ“œRead More: Python range() Function β€” A Helpful Illustrated Guide

With the prerequisites out of our way, let us dive into the mission-critical question and learn the different ways of solving the given problem.

Method 1: Using range with a Negative Step

You can use the range() function with a negative step size. The meaning is “move from right to the left” using the negative step size as the difference between two subsequent values. In this case, the start argument should be larger than the stop argument.

Thus, here’s how you can exploit the capabilities of the negative range to solve the given problem:

Solution:

for i in range(5, 0, -1):
    print(i, end=" ")
# 5 4 3 2 1 

Note that the start value in this case begins from the end. In other words the highest value of the sequence is the start value for the range function with a negative step.

Another way of implementing the negative step is as follows:

for i in range(1, 6)[::-1]:
    print(i, end=" ")
# 5 4 3 2 1

Method 2: Using reversed()

Python’s built-in reversed(sequence) function returns a reverse iterator over the values of the given sequence such as a list, a tuple, a string or a range of values. Thus, apply the reversed() function to the given range to reverse the order of the sequence.

Solution:

for i in reversed(range(1, 6)):
    print(i, end=" ")
# 5 4 3 2 1

Exercises

Well! That’s how you can reverse a range of values in Python. Let’s wrap up this tutorial with a few exercises to strengthen your grip on the topic.

Exercise 1: Consider the given snippet:

for i in range(1, 6, 2):
    print(i, end=" ")
# OUTPUT: 1 3 5

Challenge: Formulate an alternate one-line solution that reverses the given range/sequence of numbers. Thus, the expected output is: 5 3 1

Solution:

[print(i, end=" ") for i in range(5, 0, -2)]
# OUTPUT: 5 3 1 

Exercise 2: Consider that you have been given a string – “aabbbbaa”. Can you formulate a one-line solution to check if the given string is a Palindrome or not?

Solution:

print("Palindrome!" if 'aabbbbaa'[::-1] == 'aabbbbaa' else "Not Palindrome!")
# Palindrome!

Conclusion

That was a pretty simple tutorial. Wasn’t it? Nevertheless, it might come in handy in numerous situations while you are solving a specific problem. I hope this article helped you. Please subscribe and stay tuned for more interesting discussions and solutions in the future.

Here’s a list of very interesting and comprehensive tutorials that are quite similar to the one discussed here. Please feel free to dive into these tutorials to enhance your skills:


But before we move on, I’m excited to present you my new Python book Python One-Liners (Amazon Link).

If you like one-liners, you’ll LOVE the book. It’ll teach you everything there is to know about a single line of Python code. But it’s also an introduction to computer science, data science, machine learning, and algorithms. The universe in a single line of Python!

The book was released in 2020 with the world-class programming book publisher NoStarch Press (San Francisco).

Link: https://nostarch.com/pythononeliners