Python Practice › List Programs › Program 12
Reverse a list with a for loop
Write a Python program to reverse a list using a for loop.
Input
[1, 2, 3, 4, 5]
Output
[5, 4, 3, 2, 1]
Approach
- Insert each item at the front of a new list.
Solution
numbers = [1, 2, 3, 4, 5]reversed_list = []for n in numbers: reversed_list.insert(0, n)print(reversed_list)Output
[5, 4, 3, 2, 1]
Line by line
Line 4
Inserting at index 0 puts each new item before the earlier ones.
Learn the concept
New to this topic? Read the lesson, then come back.