But Python does have a built-in reversed function. If you wrap range() inside reversed() , then you can print the integers in reverse order. range() makes it possible to iterate over a decrementing sequence of numbers, whereas reversed() is generally used to loop over a sequence in reverse order.
Using the reversed() method and reverse() method, we can reverse the contents of the list object in place i.e., we don't need to create a new list instead we just copy the existing elements to the original list in reverse order. This method directly modifies the original list.
Iterate over the list using for loop and reversed() reversed() function returns an iterator to accesses the given list in the reverse order. Let's iterate over that reversed sequence using for loop i.e. It will print the wordList in reversed order.
The reverse() method reverses the elements of the list.
use reversed()
function:
reversed(range(10))
It's much more meaningful.
Update:
If you want it to be a list (as btk pointed out):
list(reversed(range(10)))
Update:
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 [5,4,3,2,1,0]
, you can use the following:
range(5, -1, -1)
It may be less intuitive but as the comments mention, this is more efficient and the right usage of range for reversed list.
Use the 'range' built-in function. The signature is range(start, stop, step)
. This produces a sequence that yields numbers, starting with start
, and ending if stop
has been reached, excluding stop
.
>>> range(9,-1,-1)
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> range(-2, 6, 2)
[-2, 0, 2, 4]
In Python 3, this produces a non-list range
object, which functions effectively like a read-only list (but uses way less memory, particularly for large ranges).
You could use range(10)[::-1]
which is the same thing as range(9, -1, -1)
and arguably more readable (if you're familiar with the common sequence[::-1]
Python idiom).
For those who are interested in the "efficiency" of the options collected so far...
Jaime RGP's answer led me to restart my computer after timing the somewhat "challenging" solution of Jason literally following my own suggestion (via comment). To spare the curious of you the downtime, I present here my results (worst-first):
Jason's answer (maybe just an excursion into the power of list comprehension):
$ python -m timeit "[9-i for i in range(10)]"
1000000 loops, best of 3: 1.54 usec per loop
martineau's answer (readable if you are familiar with the extended slices syntax):
$ python -m timeit "range(10)[::-1]"
1000000 loops, best of 3: 0.743 usec per loop
Michał Šrajer's answer (the accepted one, very readable):
$ python -m timeit "reversed(range(10))"
1000000 loops, best of 3: 0.538 usec per loop
bene's answer (the very first, but very sketchy at that time):
$ python -m timeit "range(9,-1,-1)"
1000000 loops, best of 3: 0.401 usec per loop
The last option is easy to remember using the range(n-1,-1,-1)
notation by Val Neekman.
reverse
because the range
function can return reversed list.When you have iteration over n items and want to replace order of list returned by range(start, stop, step)
you have to use third parameter of range which identifies step
and set it to -1
, other parameters shall be adjusted accordingly:
stop
parameter as -1
(it's previous value of stop - 1
, stop
was equal to 0
).n-1
.So equivalent of range(n)
in reverse order would be:
n = 10
print range(n-1,-1,-1)
#[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
for i in range(8, 0, -1)
will solve this problem. It will output 8 to 1, and -1 means a reversed list
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With