#99: Iterate in Reversed Order Through Your Lists
For a feature I build I needed a simple way to reverse the direction the for-statement iterates through a list. As it turns out, Python has a built-in function called reversed() that does exactly that.
Iterate forward
When we iterate over a list, the iterator gives us the elements in the same order as we inserted them into the list:
We could change this behaviour by creating our own iterator. While this may be necessary with a complex requirement, it is not needed to change the direction of the iterator.
Iterate backwards
The built-in function reversed() changes the direction of the iterator without changing the underlying data:
Conclusion
If you need to run in the opposite direction through an iteratable (like a list), reversed() is the function that does exactly what you need.