all Python 3Built-in functionsterm
reversed
iterators
Returns a reverse iterator over a sequence. Does not copy the data — evaluated lazily.
Syntax
reversed(sequence)
Not every iterable qualifies: reversed needs a sequence — one that defines __reversed__, or both __len__ and __getitem__. Sets and generators raise TypeError, while dicts (since 3.8) walk their keys in reverse insertion order. Unlike the [::-1] slice it makes no copy, so mutating the underlying list during iteration is visible to the iterator.
Examples
print(list(reversed([1, 2, 3, 4, 5]))) # → [5, 4, 3, 2, 1]
print(list(reversed('hello'))) # → ['o', 'l', 'l', 'e', 'h']
print(list(reversed(range(5)))) # → [4, 3, 2, 1, 0]
print(''.join(reversed('hello'))) # → olleh
for x in reversed([1, 2, 3]):
print(x, end=' ') # → 3 2 1
See also
Open in the full glossarydocs.python.orgReport an inaccuracy