PythonMastery
Python programs · Lists · beginner

Reverse a list in Python

Three ways to reverse a list in Python: slicing with [::-1] for a new list, reverse() to change the list in place, and reversed() for a loop.

Python has three ways to reverse a list. The difference is whether you get a new list or change the original one.

The program

python
numbers = [1, 2, 3, 4, 5]

backwards = numbers[::-1]
print("Reversed copy:", backwards)
print("Original:     ", numbers)

numbers.reverse()
print("After reverse():", numbers)
output
Reversed copy: [5, 4, 3, 2, 1]
Original:      [1, 2, 3, 4, 5]
After reverse(): [5, 4, 3, 2, 1]

How it works

Looping backwards

When you only want to loop over the items from last to first, reversed() does that without making a copy of the list:

python
for name in reversed(["Ada", "Grace", "Hedy"]):
    print(name)
output
Hedy
Grace
Ada

reversed() gives an iterator, not a list. Wrap it in list(...) if you need a list: list(reversed(numbers)).

Learn the idea: Lists & Sequences, Loops: for, while, and the Pythonic Patterns