Python Practice › List Programs › Program 13

Reverse a list with a while loop

EasyList Programs · 13 of 100

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

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

Approach

  1. Walk an index from the last position down to 0.

Solution

Python
numbers = [1, 2, 3, 4, 5]result = []i = len(numbers) - 1while i >= 0:    result.append(numbers[i])    i -= 1print(result)
Output
[5, 4, 3, 2, 1]

Learn the concept

New to this topic? Read the lesson, then come back.

More list programs

← All 100 List Programs

Leave a Comment