In Python, reversed(seq) returns an iterator that yields the items of a sequence from last to first. It does not copy the data and does not change the original. Wrap it in list() or "".join() to get a reversed copy.
It works with anything that has a __reversed__() method or supports len() and indexing: lists, tuples, strings, ranges, and, since Python 3.8, dicts and their views. Like every iterator, the result can be used only once.
reversed is a built-in type (a class) that you call like a function: each call creates a new reversed object. Like every built-in, it lives in the builtins module and is always available without an import.
Syntax
Usually used directly in a for loop.
python
for item in reversed(sequence):
...
Call
What it does
list(reversed([1, 2, 3]))
Returns [3, 2, 1].
"".join(reversed("abc"))
Returns "cba".
reversed(range(5))
Counts 4, 3, 2, 1, 0.
list(reversed(d))
Dict keys, last inserted first (3.8+).
seq[::-1] makes a reversed copy in one step; reversed() avoids the copy, which matters for big lists.
Library declarations
python
reversed(seq, /)
# class reversed: returns a reverse iterator
Parameters
Parameter
Description
seq
Required. A sequence (has __len__() and __getitem__()) or an object with __reversed__().
Return Value
Return Value
Description
iterator
A reverse iterator. Print list(...) of it, not the iterator itself.
Python Examples
Example 1 — A Reverse Iterator Is Used Once
Reverse a list and a string, and see that the iterator empties after one pass.
An iterator, not a list. The type name shows list_reverseiterator; nothing is copied yet.
Consumed once. The first list() takes all items, so the second gets none.
Strings. Joining the reversed characters of "stressed" spells "desserts".
Original intact.letters is unchanged, unlike letters.reverse().
Example 2 — Loop Backwards with Numbers
Count down with a reversed range and number the steps of a list from the end.
python
for i in reversed(range(1, 4)):
print("T-minus", i)
steps = ["mix", "bake", "cool"]
for number, step in enumerate(reversed(steps), 1):
print(number, step)
print(list(reversed((1, 2, 3))))