reversed()
Returns an iterator that yields items in reverse order (no copy).
it = reversed(sequence)This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Returns an iterator that yields items in reverse order (no copy).
The reversed() built-in function returns a reverse iterator for a given sequence. Unlike the list.reverse() method, which modifies a list in-place and returns None, reversed() creates a new iterator object that yields elements in reverse order without duplicating the original data. This is highly memory-efficient (O(1) auxiliary space complexity) because it doesn't create a shallow or deep copy of the sequence. To use reversed(), the target object must either implement the __reversed__() magic method or support the sequence protocol (containing both __len__() and __getitem__() methods). Note that because it returns an iterator, the result is 'lazy' and can only be consumed once. To convert the result back into a list, string, or tuple, you must explicitly pass the iterator to a constructor like list() or join it with a string.
Quick reference
Syntax
it = reversed(sequence)
Inputs
Parameters
See it in practice
Examples
Reversing a List into a New List
items = ['apple', 'banana', 'cherry']
reversed_items = list(reversed(items))
print(reversed_items)['cherry', 'banana', 'apple']
Uses the list() constructor to consume the reverse iterator and create a new list object.
Reversing a String
text = 'Python'
reversed_text = ''.join(reversed(text))
print(reversed_text)nohtyP
Strings are sequences. Since reversed() returns an iterator, the join() method is used to concatenate the characters back into a string.
Memory-Efficient Iteration
for num in reversed(range(1, 4)):
print(num)3 2 1
Iterates directly over the reverse iterator, which is memory-efficient for large ranges or lists as no intermediate copy is made.
Debug faster
Common Errors
TypeError
Cause: Attempting to reverse an object that does not support the sequence protocol (like a set or a basic dictionary).
Fix: Convert the collection to a list first, or use a collection type that maintains order and supports indexing.
reversed({1, 2, 3}) # Raises TypeError: 'set' object is not reversibleLogicError
Cause: Attempting to access elements by index or check the length of the result of reversed() directly.
Fix: Cast the result to a list() if indexing or multiple passes are required.
rev = reversed([1, 2, 3])
print(rev[0]) # Raises TypeError: 'list_reverseiterator' object is not subscriptableRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Returns an iterator that yields items in reverse order (no copy).
sequence: A sequence that supports reverse iteration.
TypeError: Convert the collection to a list first, or use a collection type that maintains order and supports indexing. LogicError: Cast the result to a list() if indexing or multiple passes are required.