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.

Answer from Greg Hewgill on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how can i loop through an array forward and then backwards and then forwards and... etc with the modulus operator?
r/learnpython on Reddit: How can I loop through an array forward and then backwards and then forwards and... etc with the modulus operator?
September 9, 2021 -

Say I have an array:

arr = [1,2,3,4,5]

and I want to output this using a simple counter and the modulus operator

1 
2
3
4 
5 
4
3 
2
1 
2 
3 
4 
5 
... 

Is this possible? I know that you can loop through an array and start over at the beginning by doing

arr[count % len(arr)]

but how do I just switch directions instead of going back to the beginning?

Discussions

Can a for loop iterate over a list backwards?
Question Is it possible to have a for loop iterate over a list in reverse order without changing the list? Answer Yes, you can iterate over a list in reverse order without changing the list using the reversed() function. In this code, the list is printed in backward order but after the for ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
13
August 3, 2018
Why isn't my loop iterating backwards in python?
Hint: Look up documentation on the ‘range’ function…does it actually do what you think it does given the two arguments you provided? Maybe you’re missing an argument? https://docs.python.org/3/library/stdtypes.html#range More on reddit.com
🌐 r/learnprogramming
13
0
March 8, 2023
How can I iterate over a list backward?
Question In the context of this code challenge, how can I iterate over a list backward? Answer There are a few ways that you can iterate over a list backward. One way is by utilizing the range() method, with a negative step value, so that the index decreases by 1 per element, essentially going ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
16
November 3, 2018
Python iterate through a multidimensional numpy array in different ways - Stack Overflow
Now I want to iterate through this Array. First I want the output be a list or numpy array like this. More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › backward-iteration-in-python
Backward iteration in Python - GeeksforGeeks
July 11, 2025 - s = "Python" # - len(s) - 1: Start at the last index # - -1: Ensures the loop stops after the first character # - -1: The step value to iterate backwards for i in range(len(s) - 1, -1, -1): print(s[i]) ... We can use slicing slicing notation [::-1] to reverse the order of elements in an iterable. This works for lists, tuples, and strings, but not for sets or dictionaries (since they are unordered). ... a = [1, 2, 3, 4, 5] #Loop through the List # reverse order using slicing(::-1) for item in a[::-1]: print(item) # same logic written using List comprehension # [print(item) for item in a[::-1]]
🌐
Real Python
realpython.com › python-reverse-list
Reverse Python Lists: Beyond .reverse() and reversed() – Real Python
June 28, 2023 - This loop iterates through a range object that goes from 0 to len(digits) // 2. Each iteration swaps an item from the first half of the list with its mirror counterpart in the second half.
🌐
Stack Abuse
stackabuse.com › bytes › traversing-a-list-in-reverse-order-in-python
Traversing a List in Reverse Order in Python
August 30, 2023 - The reversed() function is the most Pythonic way to reverse a list. It's simple, readable, and efficient. It's especially useful when you need to iterate over the list in reverse order, but don't want to modify the original list.
🌐
Steemit
steemit.com › python › @makerhacks › reversing-iteration-going-through-a-list-forwards-and-backwards-in-python
Reversing Iteration: Going through a list forwards and backwards in Python — Steemit
May 29, 2018 - One of the things Python lacks is the in-built ability to go back one item when iterating through a list. Yes, you can reverse the whole list, or work from the end, but if you are in the middle of the list you can not just skip back one item. There is no undo. Fortunately, you CAN iterate via the index number of the array...
🌐
Medium
medium.com › becoming-a-better-software-developer › undetstanding-python-iterators-reversed-iteration-35f1c1d16196
Undetstanding Python Iterators: Reversed Iteration | by Saleem Latif | Becoming a better Software Developer | Medium
March 8, 2023 - Reversed iteration is a technique that allows you to iterate over a sequence of elements in reverse order. In Python, you can reverse iterate over a sequence by using the reversed() function.
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python for loop in backwards
Python For Loop in Backwards - Spark By {Examples}
May 31, 2024 - Usually loop iterations start from front side if you want to get the loop iterations from back side rather than the front side you can use reversed() function. Pass range() function into reversed() function will reverse the range() function ...
🌐
TutorialsPoint
tutorialspoint.com › backward-iteration-in-python
Backward iteration in Python
August 7, 2019 - This method involves slicing the list which starts from the position -1 and go backwards till the first position. We use a for loop with an iterator used as the index of the element in the list.
🌐
Quora
quora.com › Python-How-do-I-iterate-over-a-sequence-in-reverse-order
Python: How do I iterate over a sequence in reverse order? - Quora
Answer (1 of 4): What sort of sequence ? If the sequence is slicable (ie a list, or a tuple or s string or something that behaves like One of the above) then you can reverse slice : [code]for elem in sequence[::-1]: print(elem) [/code]If the sequence isn’t slicable (for instance it is a gene...
🌐
Codecademy
codecademy.com › article › how-to-reverse-a-list-in-python
How to Reverse a List in Python | Codecademy
This example demonstrates using reversed() to reverse a mixed data type list and iterate through reversed elements without creating a new list:
🌐
Python Morsels
pythonmorsels.com › looping-in-reverse
Looping in reverse - Python Morsels
May 27, 2025 - If you're working with a list, a string, or any other sequence in Python, you can reverse that sequence using Python's slicing syntax: >>> colors = ["purple", "blue", "green", "pink", "red"] >>> colors[::-1] ['red', 'pink', 'green', 'blue', 'purple'] The syntax looks weird, but it does work. If we wanted to write a for loop that iterated over our list from the end to the beginning, we could loop over the reversed slice of that list:
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
Can a for loop iterate over a list backwards? - Python FAQ - Codecademy Forums
August 3, 2018 - Question Is it possible to have a for loop iterate over a list in reverse order without changing the list? Answer Yes, you can iterate over a list in reverse order without changing the list using the reversed() function.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › reverse an array in python
Reverse an Array in Python - Spark By {Examples}
May 31, 2024 - To iterate range(len(arr)-1, -1, -1) use for loop, it will return an array of elements in the reversing order. Related: In Python, you can use for loop to iterate iterable objects in reversing order and get them in backwards directions.
🌐
Codecademy Forums
discuss.codecademy.com › frequently asked questions › python faq
How can I iterate over a list backward? - Python FAQ - Codecademy Forums
November 3, 2018 - Answer There are a few ways that you can iterate over a list backward. One way is by utilizing the range() method, with a negative step value, so that the index decreases by 1 per element, essentially going backward in the list.
🌐
Towards Data Science
towardsdatascience.com › home › latest › how to reverse python lists more efficiently
How To Reverse Python Lists More Efficiently | Towards Data Science
January 20, 2025 - Return a reverse [iterator](https://docs.python.org/3/glossary.html#term-iterator). seq must be an object which has a __reversed__() method or supports the sequence protocol (the __len__() method and the __getitem__() method with integer arguments starting at 0). This approach is recommended when it comes to performing backwards iteration over a list.
🌐
W3Schools
w3schools.com › python › ref_list_reverse.asp
Python List reverse() Method
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... The reverse() method reverses the sorting order of the elements. ... The built-in function reversed() returns a reversed iterator ...