Python 3 reference · Built-in Functions

Python reversed() Function

Return an iterator that goes through a sequence backwards, without copying it. Works on lists, tuples, strings and ranges.

Python 3 Since 3.0

Syntax

reversed(seq)

Parameters

ParameterDescription
seq A sequence (supports len() and indexing) or an object with __reversed__().

Return value

A reverse iterator.

Examples

Go backwardsTry it yourself
print(list(reversed([1, 2, 3])))
print("".join(reversed("stressed")))
for n in reversed(range(3)):
    print(n)

Output

[3, 2, 1]
desserts
2
1
0
Other ways to reverseTry it yourself
items = [1, 2, 3]
print(items[::-1])             # a reversed copy
items.reverse()                # reverse in place
print(items)

Output

[3, 2, 1]
[3, 2, 1]

Good to know

reversed() doesn't work on sets, dict views of older Pythons, or generators: they aren't sequences. Use sorted(..., reverse=True) or list(...)[::-1] instead.

Version history

VersionChange
Python 3.0Added