In this Python tutorial, we will learn how to reverse a list using a for loop. The examples show how to read elements from the last index to the first, store them in a new list, and reverse a list without using the built-in reverse() method.
Reverse a Python List Using a For Loop
To reverse a list using a for loop, iterate through the original list from its last index to its first index. During each iteration, append the current element to a new list.
If the list contains n elements, its valid indexes range from 0 to n - 1. Therefore, the element at index len(myList) - i - 1 moves backward through the list as i increases.
Python Program to Reverse a List with Indexes
In the following Python program, we initialize a list with string values and reverse it using a for loop. The original list remains unchanged because the reversed elements are stored in a separate list.
main.py
#initialize list
myList = ['apple', 'banana', 'cherry', 'mango']
#store reversed list in this
reversedList = []
#reverse list using for loop
for i in range(len(myList)) :
reversedList.append(myList[len(myList) - i - 1])
#print lists
print(f'Original List : {myList}')
print(f'Reversed List : {reversedList}')
Output
Original List : ['apple', 'banana', 'cherry', 'mango']
Reversed List : ['mango', 'cherry', 'banana', 'apple']
The loop runs once for each element. On the first iteration, it reads the last element, 'mango'. It then reads 'cherry', 'banana', and 'apple' in that order.
How the Reverse List Index Formula Works
The expression used to access each element is:
myList[len(myList) - i - 1]
For a list with four elements, len(myList) returns 4. The indexes generated by the expression are:
| Value of i | Calculated index | Element |
|---|---|---|
0 | 4 - 0 - 1 = 3 | 'mango' |
1 | 4 - 1 - 1 = 2 | 'cherry' |
2 | 4 - 2 - 1 = 1 | 'banana' |
3 | 4 - 3 - 1 = 0 | 'apple' |
Reverse a Python List Using range() with a Negative Step
You can also generate the indexes directly in descending order. Start at the last valid index, stop before -1, and use -1 as the step.
myList = ['apple', 'banana', 'cherry', 'mango']
reversedList = []
for index in range(len(myList) - 1, -1, -1):
reversedList.append(myList[index])
print(reversedList)
Output
['mango', 'cherry', 'banana', 'apple']
For this example, range(3, -1, -1) produces the indexes 3, 2, 1, and 0.
Reverse a Python List Using Negative Indexes
Python supports negative indexing. Index -1 refers to the last element, -2 refers to the second-last element, and so on. This can make the loop easier to read.
numbers = [10, 20, 30, 40]
reversedNumbers = []
for position in range(1, len(numbers) + 1):
reversedNumbers.append(numbers[-position])
print(reversedNumbers)
Output
[40, 30, 20, 10]
Reverse a List In Place Using a For Loop
The previous examples create a new list. To reverse the original list itself, swap the first and last elements, then the second and second-last elements, continuing until the middle is reached.
myList = ['apple', 'banana', 'cherry', 'mango']
for i in range(len(myList) // 2):
oppositeIndex = len(myList) - i - 1
myList[i], myList[oppositeIndex] = myList[oppositeIndex], myList[i]
print(myList)
Output
['mango', 'cherry', 'banana', 'apple']
The loop only needs to run for half the list because each iteration moves two elements to their reversed positions. This version modifies myList directly and does not create another list containing all the elements.
Reverse an Empty List or a Single-Element List
The same loop logic also works for an empty list and a list containing one element. There are no elements to rearrange in either case.
lists = [[], ['apple']]
for myList in lists:
reversedList = []
for index in range(len(myList) - 1, -1, -1):
reversedList.append(myList[index])
print(reversedList)
Output
[]
['apple']
For Loop Reversal Compared with Other Python List Methods
A for loop is useful when learning how indexes work or when custom processing must be performed for every element. Python also provides shorter ways to reverse a list.
| Approach | Modifies original list | Creates a new list |
|---|---|---|
For loop with append() | No | Yes |
| For loop with element swapping | Yes | No |
list.reverse() | Yes | No |
myList[::-1] | No | Yes |
reversed(myList) | No | Returns an iterator |
Use a loop when the reversal itself is part of the learning objective or when each element requires additional processing. For ordinary application code, reverse(), slicing, or reversed() may be more concise.
Reference Tutorials for Reversing the List
Summary of Reversing a Python List with a For Loop
In this Python Tutorial, we learned how to reverse a list using a for loop. We created a new reversed list by reading indexes from right to left, used negative indexes, and reversed the original list in place by swapping opposite elements.
TutorialKart.com