ordering
list.reverse
Reverses the elements of the list in place. Returns None; O(n) time and O(1) extra space.
list.reverse() Parameters
| Parameter | Purpose |
|---|---|
| (no args) | reverse() takes no arguments. |
| vs. lst[::-1] | Slicing with ::-1 returns a new reversed list without touching the original. |
Examples
>>> lst = [1, 2, 3]
>>> lst.reverse()
>>> lst
[3, 2, 1] In-place reversal.
>>> lst = [1, 2, 3]
>>> lst[::-1]
[3, 2, 1]
>>> lst
[1, 2, 3] Slice copy leaves the original unchanged.
>>> lst = [1, 2, 3]
>>> list(reversed(lst))
[3, 2, 1] reversed() returns an iterator — cheaper if you only need one pass.
>>> [].reverse()
>>> Safe on empty lists; no exception.
Gotcha
reverse() returns None (like sort). Use lst[::-1] for a new list or reversed(lst) for a lazy iterator when you must not mutate the original.
Related methods
list.sort
Sorts the list in place using the stable Timsort algorithm; returns None. Elements must be pairwise comparable unless a key function reduces them to a comparable value.
sorted (built-in)
Built-in function that returns a NEW sorted list from any iterable, leaving the source untouched. Uses the same stable Timsort and key/reverse arguments as list.sort.
list[i] / list[i:j:k]
Subscript syntax to read a single element (by index) or a new list (by slice). Slicing always returns a new shallow copy; out-of-range slice bounds are silently clamped while single-index access raises IndexError.
← All Python list methods · Dict methods · list vs tuple vs set