Python Practice › List Programs › Program 12

Reverse a list with a for loop

EasyList Programs · 12 of 100

Write a Python program to reverse a list using a for loop.

Advertisement
Input
[1, 2, 3, 4, 5]
Output
[5, 4, 3, 2, 1]

Approach

  1. Insert each item at the front of a new list.

Solution

Python
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.

More list programs

← All 100 List Programs

Leave a Comment