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
numbers[::-1]is a slice with a step of -1. It walks the list from the end to the start and builds a new list. The original list does not change.numbers.reverse()changes the list itself (in place) and returnsNone. Soprint(numbers.reverse())printsNone, which surprises many beginners.
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)).