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 Posts:
- What is the difference between range and xrange functions in Python 2.X?
- what does +=, -=, *= and /= stand for in Python?
- When to use “while” or “for” in Python
- python “TypeError: ‘numpy.float64’ object cannot be interpreted as an integer”
- Python – TypeError: ‘int’ object is not iterable
- Why there is no do while loop in python
- Python – TypeError: ‘int’ object is not iterable
- Accessing the index in ‘for’ loops?
- Accessing the index in ‘for’ loops?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- How to avoid “RuntimeError: dictionary changed size during iteration” error?
- Print a list in reverse order with range()?
- How to iterate over a JavaScript object?
- Python list sort in descending order
- Pythonic way to combine FOR loop and IF statement
- Subplot for seaborn boxplot
- Exit while loop in Python
- While Loop in Mips (New to Mips)
- Pythonic way to iterate through a range starting at 1
- While, Do While, For loops in Assembly Language (emu8086)
- Angular 2 Cannot find control with unspecified name attribute on formArrays
- Loop backwards using indices in Python?
- Why does using from __future__ import print_function breaks Python2-style print?
- Guess a number program with Java
- Breaking out of nested loops
- Is there a “do … until” in Python?
- Iterating over a 2 dimensional python list [duplicate]
- Infinite for loops possible in Python?
- python: restarting a loop
- What is the best way of determining a loop invariant?
- Why does Lua have no “continue” statement?
- creating a reverse method for a python list from scratch
- How do you write code to do factorials in MIPS?
- Python loop counter in a for loop
- Get a Try statement to loop around until correct value obtained
- Printing from a declared array in MIPS
- Python: Continuing to next iteration in outer loop
- TypeError: only size-1 arrays can be converted to Python scalars (matplotlib)
- Why do I get TypeError: can’t multiply sequence by non-int of type ‘float’?
- Writing a pandas DataFrame to CSV file
- How do I install pip on macOS or OS X?
- What is the necessity of plt.figure() in matplotlib?
- When should I use uuid.uuid1() vs. uuid.uuid4() in python?
- ImportError: No module named tensorflow
- Best way to convert string to bytes in Python 3?
- How do you change the size of figures drawn with Matplotlib?
- Error: all the input array dimensions except for the concatenation axis must match exactly
- Expected 2D array, got 1D array instead error
- syntaxerror: “unexpected character after line continuation character in python” math
- How to prevent errno 32 broken pipe?
- The equivalent of a GOTO in python [duplicate]
- tkinter gui with progress bar
- Invalid character in identifier
- if else in a list comprehension
- What does “\r” do in the following script?
- Convert list to tuple in Python
- How to enable CORS in flask
- Python: TypeError: object of type ‘NoneType’ has no len()
- anaconda – path environment variable in windows
- Pandas: ValueError: cannot convert float NaN to integer
- How to find all occurrences of an element in a list
- convert json ipython notebook(.ipynb) to .py file
- how to update spyder on anaconda
- Django upgrading to 1.9 error “AppRegistryNotReady: Apps aren’t loaded yet.”
- Pygame Drawing a Rectangle
- Purpose of `numpy.log1p( )`?
- How to make a 3D scatter plot in matplotlib
- Arrays used as indices must be of integer (or boolean) type
- How can I compare two lists in python and return matches
- How to draw vertical lines on a given plot in matplotlib
- Changing the “tick frequency” on x or y axis in matplotlib
- mysql_config not found when installing mysqldb python interface
- PermissionError: [WinError 5] Access is denied python using moviepy to write gif
- How can I selectively escape percent (%) in Python strings?
- “ImportError: No module named site” on Windows
- How do you read a file into a list in Python?
- How to find elements by class
- Euclidean Algorithm / GCD in Python
- Pandas split DataFrame by column value
- Using numpy to build an array of all combinations of two arrays
- Get a random sample with replacement
- Can’t update to numpy 1.13 with anaconda?
- Is there any simple way to benchmark Python script?
- Purpose of python antigravity module
- Error: “dictionary update sequence element #0 has length 1; 2 is required” on Django 1.4
- Flask-SQLalchemy update a row’s information
- Inline for loop
- Read data (.dat file) with Pandas
- Is there any numpy group by function?
- What is the correct format to write float value to file in Python
- How is Python’s List Implemented?
- ImportError: No module named IPython
- How to find length of digits in an integer?
- What exactly do “u” and “r” string flags do, and what are raw string literals?
- “E: Unable to locate package python3-pip”
- Compare two columns using pandas
- ValueError: total size of new array must be unchanged
- Shuffle an array with python, randomize array item order with python
- Separating html and JavaScript in Flask [duplicate]
- No handlers could be found for logger