In this Python tutorial, we will learn how to reverse a given list of elements using a For loop statement.
Reverse a List using For Loop
To reverse a list of elements in Python, iterate over the list from the end to start, and append each of the iterated element in a new list.
Program
In the following python program, we initialize a python list with some string values, and reverse the list using a for loop.
main.py
</>
Copy
#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']
References tutorials for the above program
Conclusion
In this Python Tutorial, we learned how to reverse a list using for loop.