Python Practice › List Programs › Program 14

Reverse a list with slicing

EasyList Programs · 14 of 100

Write a Python program to reverse a list using slicing.

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

Approach

  1. [::-1] returns a reversed copy.

Solution

Python
numbers = [1, 2, 3, 4, 5]print(numbers[::-1], numbers)
Output
[5, 4, 3, 2, 1] [1, 2, 3, 4, 5]

Line by line

Line 2

The slice creates a new list; the original is unchanged.

Learn the concept

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

More list programs

← All 100 List Programs

Leave a Comment