Skip to content

Python range reverse | Example code

  • by

Simple 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 or suggestions on this Python range topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading