Use reversed() function (efficient since range implements __reversed__):

reversed(range(10))

It's much more meaningful.

Update: list cast

If you want it to be a list (as @btk pointed out):

list(reversed(range(10)))

Update: range-only solution

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

For example, to generate a list [3, 2, 1, 0], you can use the following:

range(3, -1, -1)

It may be less intuitive, but it works the same with less text. This answer by @Wolf indicates this approach is slightly faster than reversed.

Answer from Michał Šrajer on Stack Overflow
🌐
LearnPython.com
learnpython.com › blog › reverse-range-in-python
How to Reverse a Range in Python | LearnPython.com
September 7, 2022 - Learn how to reverse a range in Python using the range(), reversed(), and sorted() functions.
Discussions

Reversing a range using a FOR LOOP
You should show what you've tried so far. It's easier to give productive help if we base the help on code you've written. It doesn't require a for-loop though. You can use one, but it would be far more straightforward with a while. Be careful of artificially limiting what tools you consider. More on reddit.com
🌐 r/learnpython
11
0
December 24, 2022
Reverse Range in Swift - Stack Overflow
Is there a way to work with reverse ranges in Swift? For example: for i in 5...1 { // do something } is an infinite loop. In newer versions of Swift that code compiles, but at runtime gives the More on stackoverflow.com
🌐 stackoverflow.com
linear algebra - Reverse range of numbers, scaling - Mathematics Stack Exchange
I have a float that goes from 1 to 0 .Im trying to make it so that the order is reversed and scaled so it goes from 0 to -80 Just wondering if there is a straight forward way to do this? More on math.stackexchange.com
🌐 math.stackexchange.com
How do I reverse a range?
I wanted to construct a more general range which can also count downward. I tried the following let range = if start More on users.rust-lang.org
🌐 users.rust-lang.org
19
2
May 18, 2019
People also ask

Are there any performance considerations when using reverse range?
Reverse range doesn't have any significant performance differences compared to the regular range. Both functions have similar time complexities, and the choice between them depends on the specific requirements of your program. However, it's important to be mindful of the step size, especially when working with large ranges, to avoid unintended results or excessive memory usage.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Python › python-reverse-range
How to Use Python Reverse Range: Easy Guide – Kanaries
Can I combine reverse range with other Python functions or libraries?
Absolutely! Python reverse range can be seamlessly integrated with other Python functions and libraries. For example, you can combine reverse range with list comprehensions or apply it within NumPy functions to perform complex calculations on reverse sequences.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Python › python-reverse-range
How to Use Python Reverse Range: Easy Guide – Kanaries
Can I use reverse range with non-numeric values, such as strings or objects?
No, reverse range is specifically designed for numerical sequences. It operates on numbers and doesn't provide a direct way to reverse non-numeric values. However, you can still use other techniques, like indexing or slicing, to achieve reverse iteration with non-numeric sequences.
🌐
docs.kanaries.net
docs.kanaries.net › topics › Python › python-reverse-range
How to Use Python Reverse Range: Easy Guide – Kanaries
🌐
Flexiple
flexiple.com › python › python-range-reverse
How to reverse a range in Python? | Flexiple Tutorials | Python - Flexiple
For this, we use the reversed() function in Python. This function reverses the arguments passed. ... This is how you can use Python range to reverse a list of numbers.
🌐
Kanaries
docs.kanaries.net › topics › Python › python-reverse-range
How to Use Python Reverse Range: Easy Guide – Kanaries
June 6, 2023 - Python's range() function allows you to generate a sequence of numbers, starting from a specified value, ending before another specified value, and incrementing by a specified step. Reverse range can be achieved by setting the start value greater than the stop value and using a negative step.
🌐
Real Python
realpython.com › python-range
Python range(): Represent Numerical Ranges – Real Python
November 24, 2024 - In Python, the range() function generates a sequence of numbers, often used in loops for iteration. By default, it creates numbers starting from 0 up to but not including a specified stop value. You can also reverse the sequence with reversed().
🌐
Replit
replit.com › home › discover › how to reverse a range in python
How to reverse a range in Python | Replit
March 3, 2026 - In the expression range(4, -1, -1), the arguments work like this: The start value (4) is where the sequence begins. The stop value (-1) is the boundary. The loop ends before reaching this number. The step value (-1) decrements the count by one in each iteration. This approach gives you direct control over the sequence's start, end, and direction. reversed_range = list(range(5))[::-1] print(reversed_range)--OUTPUT--[4, 3, 2, 1, 0]
Find elsewhere
🌐
Enterprise DNA
blog.enterprisedna.co › how-to-reverse-a-range-in-python
5 Way How to Reverse a Range in Python: A Step-By-Step Guide – Master Data Skills + AI
The range() function in Python is helpful in generating a sequence of numbers within a given range. By incorporating reverse techniques using functions like reversed() or adding a negative step in the range() function itself, we can effectively create a reverse range.
Top answer
1 of 6
218

Update For latest Swift 3 (still works in Swift 4)

You can use the reversed() method on a range

for i in (1...5).reversed() { print(i) } // 5 4 3 2 1

Or stride(from:through:by:) method

for i in stride(from:5,through:1,by:-1) { print(i) } // 5 4 3 2 1

stride(from:to:by:) is similar but excludes the last value

for i in stride(from:5,to:0,by:-1) { print(i) } // 5 4 3 2 1

Update For latest Swift 2

First of all, protocol extensions change how reverse is used:

for i in (1...5).reverse() { print(i) } // 5 4 3 2 1

Stride has been reworked in Xcode 7 Beta 6. The new usage is:

for i in 0.stride(to: -8, by: -2) { print(i) } // 0 -2 -4 -6
for i in 0.stride(through: -8, by: -2) { print(i) } // 0 -2 -4 -6 -8

It also works for Doubles:

for i in 0.5.stride(to:-0.1, by: -0.1) { print(i) }

Be wary of floating point compares here for the bounds.

Earlier edit for Swift 1.2: As of Xcode 6 Beta 4, by and ReverseRange don't exist anymore :[

If you are just looking to reverse a range, the reverse function is all you need:

for i in reverse(1...5) { println(i) } // prints 5,4,3,2,1

As posted by 0x7fffffff there is a new stride construct which can be used to iterate and increment by arbitrary integers. Apple also stated that floating point support is coming.

Sourced from his answer:

for x in stride(from: 0, through: -8, by: -2) {
    println(x) // 0, -2, -4, -6, -8
}

for x in stride(from: 6, to: -2, by: -4) {
    println(x) // 6, 2
}
2 of 6
27

There's something troubling about the asymmetry of this:

for i in (1..<5).reverse()

...as opposed to this:

for i in 1..<5 {

It means that every time I want to do a reverse range, I have to remember to put the parentheses, plus I have to write that .reverse() on the end, sticking out like a sore thumb. This is really ugly in comparison to C-style for loops, which are symmetrical counting up and counting down. So I tended to use C-style for loops instead. But in Swift 2.2, C-style for loops are going away! So I've had to scurry around replacing all my decrementing C-style for loops with this ugly .reverse() construct — wondering all the while, why on earth isn't there a reverse-range operator?

But wait! This is Swift — we're allowed to define our own operators!! Here we go:

infix operator >>> {
    associativity none
    precedence 135
}

func >>> <Pos : ForwardIndexType where Pos : Comparable>(end:Pos, start:Pos)
    -> ReverseRandomAccessCollection<(Range<Pos>)> {
        return (start..<end).reverse()
}

So now I'm allowed to say:

for i in 5>>>1 {print(i)} // 4, 3, 2, 1

This covers just the most common case that occurs in my code, but it is far and away the most common case, so it's all I need at present.

I had a kind of internal crisis coming up with the operator. I would have liked to use >.., as being the reverse of ..<, but that's not legal: you can't use a dot after a non-dot, it appears. I considered ..> but decided it was too hard to distinguish from ..<. The nice thing about >>> is that it screams at you: "down to!" (Of course you're free to come up with another operator. But my advice is: for super symmetry, define <<< to do what ..< does, and now you've got <<< and >>> which are symmetrical and easy to type.)


Swift 3 version (Xcode 8 seed 6):

infix operator >>> : RangeFormationPrecedence
func >>><Bound>(maximum: Bound, minimum: Bound) ->
    ReversedRandomAccessCollection<CountableRange<Bound>> 
    where Bound : Comparable, Bound.Stride : Integer {
        return (minimum..<maximum).reversed()
}

Swift 4 version (Xcode 9 beta 3):

infix operator >>> : RangeFormationPrecedence
func >>><Bound>(maximum: Bound, minimum: Bound)
    -> ReversedRandomAccessCollection<CountableRange<Bound>>
    where Bound : Comparable & Strideable { 
        return (minimum..<maximum).reversed()
}

Swift 4.2 version (Xcode 10 beta 1):

infix operator >>> : RangeFormationPrecedence
func >>><Bound>(maximum: Bound, minimum: Bound)
    -> ReversedRandomAccessCollection<Range<Bound>>
    where Bound : Strideable { 
        return (minimum..<maximum).reversed()
}

Swift 5 version (Xcode 10.2.1):

infix operator >>> : RangeFormationPrecedence
func >>><Bound>(maximum: Bound, minimum: Bound)
    -> ReversedCollection<Range<Bound>>
    where Bound : Strideable {
        return (minimum..<maximum).reversed()
}
🌐
Kodeclik
kodeclik.com › python-range-in-reverse
How to Reverse a Range in Python
September 19, 2024 - There are three ways to reverse a range in Python. 1. Use a negative step size to construct the range. 2. Use the reversed() function. 3. Construct the range the normal way and then use slicing.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › reverse a range in python with examples
Reverse a Range in Python with Examples - Spark By {Examples}
May 31, 2024 - How to reverse a range in Python? There are several ways to reverse the range of numbers, for example, by using the reversed(), and sorted() built-in
🌐
Cppreference
en.cppreference.com › w › cpp › algorithm › ranges › reverse.html
std::ranges::reverse - cppreference.com
All names in this menu belong to namespace std::ranges · [edit] 1) Reverses the order of the elements in the range [first, last). Behaves as if applying ranges::iter_swap to every pair of iterators first + i, last - i - 1 for each integer i, where 0 ≤ i < (last - first) / 2.
🌐
LearnPython.com
learnpython.com › tags › reverse-range
Reverse Range | LearnPython.com
September 7, 2022 - At some point, you may wonder ‘How would I get these values in the reverse order?’. If you think about it, we reverse ranges constantly: when we sort files in our computer from ascending to descending order, we effectively reverse their original order.
🌐
Google Groups
groups.google.com › g › golang-nuts › c › 8POFxbr_bAg
Is there an easy reverse range operator?
Channels in reverse order? Violates causality. On Wed, Oct 24, 2012 at 4:04 PM, Rémy Oudompheng <remyoud...@gmail.com> wrote: >> For arrays/slices I know I can just as easily do a for loop counting >> down, but for other types that can be ranged over, it's not as obvious >> what is the easy equivalent is.
🌐
freeCodeCamp
freecodecamp.org › news › python-reverse-list-how-to-reverse-a-range-or-array
Python Reverse List – How to Reverse a Range or Array
November 22, 2021 - You see that the initial order of the list has now changed and the elements inside it have been reversed. The slicing operator works similarly to the range() function you saw earlier on.
🌐
GoLinuxCloud
golinuxcloud.com › home › python › how to print range() in reverse order in python
How to print range() in reverse order in Python | GoLinuxCloud
December 29, 2023 - You can see in the above output that the range of numbers is starting from 2 as specified and the step size is 3. The Python reversed() function allows us to process the items in a sequence in reverse order.