Iterating through lists [SOLVED]

There’s quite a few places in my code where I have a list of things and I iterate through that list, performing actions, like this:

for I in myList:
     I.performAction()

This works really well for me, but in one particular instance, I’m wishing I could actually iterate through the list backwards, starting with the last element and going one by one through to the first element. Is there a way to do that with for loops, or do I need to use a while loop with a degrading counter?

You can do:

myList.reverse()

Before the loop. However, this will reverse the myList and not return a new, reversed version of the list. Thus in order to make sure you don’t reverse the original list permanently I would do this:

reversed = list(myList)
reversed.reverse()
for l in reversed:
    l.preformAction()

That should work, but there might be a better way of dealing with it of course.

for i in reversed(mylist):

ah, I knew there has to be a more straight forward way :slight_smile:. Thanx ynjh_jo

That’s excellent guys, thanks very much.