Python range reverse | Example code

Simple sue use reversed() function to reverse the range() function output in python. Another way is to use a negative step to create a range in reverse,

reversed(range(10))

Python range reverse list example

Simple example code reversing a range or a sequence of numbers results in a sequence containing the numbers from the range in reverse order.

res = reversed(range(5))

print(list(res))

Output:

Python range reverse

Another example (Sequence of numbers with negative step)

If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step)

ra = range(3, 0, -1)

for num in ra:
    print(num)

Output:

3
2
1

Do comment if you have any doubts and suggestions on this Python range topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.