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.
Related Contents:
- Accessing the index in ‘for’ loops
- Reverse / invert a dictionary mapping
- How to iterate over a list in chunks
- Print a list in reverse order with range()?
- How to retry after exception?
- Loop backwards using indices
- Pythonic way to combine for-loop and if-statement
- How do I loop through a list by twos? [duplicate]
- Is it possible to implement a Python for range loop without an iterator variable?
- Right way to reverse a pandas DataFrame?
- How do I count occurrence of unique values inside a list
- How can I make sense of the `else` clause of Python loops?
- How do you create different variable names while in a loop? [duplicate]
- Is there a “do … until” in Python? [duplicate]
- Python: Continuing to next iteration in outer loop
- Check if key exists and iterate the JSON array using Python
- Why does using from __future__ import print_function breaks Python2-style print? [closed]
- Numpy how to iterate over columns of array?
- How do I exchange keys with values in a dictionary? [duplicate]
- Iterating Over Dictionary Key Values Corresponding to List in Python
- Iterate through pairs of items in a Python list [duplicate]
- Why do you have to call .items() when iterating over a dictionary in Python?
- Loop through list with both content and index [duplicate]
- Best way to create a “reversed” list in Python? [duplicate]
- More Pythonic Way to Run a Process X Times [closed]
- Looping from 1 to infinity in Python
- Python – TypeError: ‘int’ object is not iterable
- How to reverse tuples in Python? [duplicate]
- Lambda in a loop [duplicate]
- Is there a difference between “pass” and “continue” in a for loop in Python?
- Strange result when removing item from a list while iterating over it
- Object does not support item assignment error
- Best way to loop over a python string backwards
- How do I sort a list of objects based on an attribute of the objects?
- How can I avoid “RuntimeError: dictionary changed size during iteration” error?
- Is it possible to make a `for` loop without an iterator variable? (How can I make make code loop a set number of times?)
- Iterate through adjacent pairs of items in a Python list [duplicate]
- Iterating over a 2 dimensional python list [duplicate]
- Updating a value in a pandas dataframe in an iterrows loop
- Python For loop get index [duplicate]
- Iterate through dictionary values?
- How to reverse order of keys in python dict?
- Strange result when removing item from a list while iterating over it in Python
- Skip multiple iterations in loop
- Python Reverse Find in String
- Why there is no do while loop in python
- Python: Make class iterable
- Idiomatic Python: ‘times’ loop [duplicate]
- When to use “while” or “for” in Python
- How to replace some characters from the end of a string?