list.reverse()
Python · Lists
Theory
.reverse() flips the order of the items of a list in place: the first item becomes the last and vice versa. sales = [100, 250, 300] then sales.reverse() gives [300, 250, 100]. It does not look at the values at all, so it is not the same as sorting in descending order; [3, 1, 2] reversed is [2, 1, 3]. It returns None and runs in O(n), simply swapping items from both ends towards the middle.
There are two other ways to get reversed data without changing the original. The slice sales[::-1] creates a new reversed list (a copy, using extra memory). The built-in reversed(sales) returns a lightweight iterator that walks the list backwards without copying, ideal in a for loop: for s in reversed(sales). Choose based on whether you need to keep the original order and whether you need a real list.
Why we use it
Data often comes oldest-first when you want newest-first, or you need to process items from the end. reverse() flips the order cheaply without building a second list.
When to use it
Use reverse() when you want the list itself flipped. Use sales[::-1] when you need a reversed copy and the original untouched, reversed() when you only loop backwards, and sort(reverse=True) when you actually want descending values.
How it works
- Call sales.reverse()
- Point at the first and last items
- Swap them and move both pointers one step inward
- Have the pointers met or crossed? Yes: Stop and return None. No: Swap the next pair.
- print(sales) shows [300, 250, 100]
Syntax
list.reverse()Example
sales = [100, 250, 300]
sales.reverse()
print(sales)Key points
- Reverses order only; does not sort by value
- Modifies the list in place and returns None
- list[::-1] returns a reversed copy without mutating the original
- reversed(list) gives an iterator for looping backwards without copying
- For descending values use sort(reverse=True)