range() and xrange() take a third parameter that specifies a step. So you can do the following.
range(10, 0, -1)
Which gives
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
But for iteration, you should really be using xrange instead. So,
xrange(10, 0, -1)
Answer from Chinmay Kanchi on Stack OverflowNote for Python 3 users: There are no separate
rangeandxrangefunctions in Python 3, there is justrange, which follows the design of Python 2'sxrange.
range() and xrange() take a third parameter that specifies a step. So you can do the following.
range(10, 0, -1)
Which gives
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
But for iteration, you should really be using xrange instead. So,
xrange(10, 0, -1)
Note for Python 3 users: There are no separate
rangeandxrangefunctions in Python 3, there is justrange, which follows the design of Python 2'sxrange.
for x in reversed(whatever):
do_something()
This works on basically everything that has a defined order, including xrange objects and lists.
Use the built-in reversed() function:
>>> a = ["foo", "bar", "baz"]
>>> for i in reversed(a):
... print(i)
...
baz
bar
foo
To also access the original index, use enumerate() on your list before passing it to reversed():
>>> for i, e in reversed(list(enumerate(a))):
... print(i, e)
...
2 baz
1 bar
0 foo
Since enumerate() returns a generator and generators can't be reversed, you need to convert it to a list first.
You can do:
for item in my_list[::-1]:
print item
(Or whatever you want to do in the for loop.)
The [::-1] slice reverses the list in the for loop (but won't actually modify your list "permanently").
Reversing a range using a FOR LOOP
How do I use reverse list in Python to loop through a list backwards? - Ask a Question - TestMu AI Community
Why isn't my loop iterating backwards in python?
iterate over a string in reverse
Videos
I'm doing self-study program in Python and I'm being challenged to create a function that act exactly like the RANGE function, but it reverse the range. Let's call it reverserange().
reverserange(0,5) will return tuple (4, 3, 2, 1, 0)
I can't for the life of me figure it out. I've been trying for a couple hours. I know it requires a for loop...
Can anyone assist?
Basically, I wanted the loop to start from the last index and work that way done. However, I am not getting anything.
listr = [10,20,30,50]
count = 0
for i in range(len(listr),-1):
count +=1
print(listr[i], count)
print('-----')I want to iterate over a string in reverse. I googled it and found this way
for i in range(len(k)-1, 0-1, -1):
but to be honest I don't understand it. can you please explain it to me?
also, I would love it if you guys can suggest other methods
So I'm building a game that requires me to loop through a list in reverse order.
It seems that there are multiple ways to do this same task, but I want to make sure I understand the differences between the alternative ways.
From what I have found online:
I can use list comprehension -
for _ in desired_list[::-1]
I can use reversed() -
for _ in revered(desired_list)
I can use a range() -
for _ in range(len(desired_list) -1, 0, -1)
Is there any functional difference between these?
Also for the range iteration, could you verify what each number stands for?
It seems to be:
range("number to start on", "number to end up", "direction and distance to travel in list")
Is that correct?
Thank you for any help!