Python Practice › List Programs › Program 14
Reverse a list with slicing
Write a Python program to reverse a list using slicing.
Input
[1, 2, 3, 4, 5]
Output
[5, 4, 3, 2, 1] [1, 2, 3, 4, 5]
Approach
[::-1]returns a reversed copy.
Solution
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.