list.reverse()
Turns a list around so the last item comes first. It changes the list itself and returns None; it does not sort.
list.reverse()reverse() flips the order of the items in a list: the first becomes the last and the last becomes the first. The list is changed in place.
It only turns the current order around. It does not sort anything: to get largest-first, use sort(reverse=True).
Like other list methods that change the list, it returns None. If you want a reversed copy and keep the original, use the slice items[::-1], or reversed() to loop backwards without making a copy at all.
Return value
Nothing (None). The list is changed in place.
Examples
1Flip a list
steps = ["wake up", "shower", "breakfast"]
steps.reverse()
print(steps)['breakfast', 'shower', 'wake up']
2reverse() does not sort
nums = [3, 9, 1, 5]
nums.reverse()
print(nums)
nums.sort(reverse=True)
print(nums)[5, 1, 9, 3] [9, 5, 3, 1]
3Three ways to go backwards
letters = ["a", "b", "c"]
print(letters[::-1])
print(list(reversed(letters)))
print(letters)
letters.reverse()
print(letters)['c', 'b', 'a'] ['c', 'b', 'a'] ['a', 'b', 'c'] ['c', 'b', 'a']
The slice and reversed() leave letters alone. Only reverse() changes it.
4Show the newest messages first
inbox = ["Hi!", "Lunch at 1?", "Running late"]
latest_first = inbox[::-1]
for msg in latest_first[:2]:
print(msg)Running late Lunch at 1?
Common mistakes
Printing or storing the result of reverse()
reverse() returns None. Call it first and print the list afterwards, or use items[::-1].
items = [1, 2, 3]
print(items.reverse())
print(items)None [3, 2, 1]
Questions people ask
How do I reverse a list in Python without changing it?
Use items[::-1] for a reversed copy, or reversed(items) to loop over it backwards.
How do I reverse a string?
Strings have no reverse() method. Use a slice: "hello"[::-1] gives 'olleh'.
How do I sort a list in reverse order?
items.sort(reverse=True). reverse() alone only flips the current order.