Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Python · Function

reversed

Returns an iterator that traverses a supported object in reverse order. It does not itself create or mutate a reversed collection.

How it works

reversed() creates a reverse iterator for a suitable object. The object must provide __reversed__(), or support the sequence protocol through __len__() and integer-indexed __getitem__().

Syntax

reversed(object, /)

Parameters

NameTypeDescription
objectobjectObject to traverse backward; it must implement __reversed__() or the required sequence protocol.

Returns

A reverse iterator.

Key points

  • The result is lazy: iteration happens through the returned reverse iterator rather than by immediately creating a new collection.
  • Objects without __reversed__() can still work if they satisfy Python's sequence protocol.
  • Sequence-protocol support specifically requires both length reporting and item access methods.
  • The fallback item lookup must accept integer indices beginning at zero.

Common mistakes

  • Arbitrary iterators are not accepted unless they provide the required reverse-iteration support; materialize one first if necessary.

Example: Iterate in reverse

numbers = [1, 2, 3]
for number in reversed(numbers):
    print(number)

Iterates over the list from its last element to its first without changing the original list.

When to use it

Reverse traversal

Process sequence elements from newest or last to first without changing the source.

Lazy iteration

Feed backward iteration into a loop or another iterator-consuming operation.

Custom containers

Provide custom reverse behavior for a container by implementing __reversed__().

Interview questions

QWhat does reversed() return, and what inputs does it accept?

reversed() returns an iterator rather than a reversed copy. It works when the input defines __reversed__(), or when it supports __len__() and integer-based __getitem__() starting at zero.

Follow-up: How can a custom container control reverse iteration? Define __reversed__() and return a new iterator that yields the container's elements in reverse order.

QHow does reversed() determine whether an object can be reversed?

The function uses a custom __reversed__() method when one exists. Otherwise, it can use the sequence protocol, requiring __len__() and integer-indexed __getitem__() beginning with index 0.

Follow-up: What should __reversed__() return? A new iterator that traverses all container objects in reverse order.

QDoes reversed() modify the original sequence?

The returned iterator produces elements from the end toward the beginning, while the original sequence remains unchanged. Converting the iterator to list is optional when a materialized result is required.

Follow-up: When is list(reversed(value)) appropriate? Use it when repeated access, indexing, or a persistent reversed collection is needed.

  • __reversed__()
  • __len__()
  • __getitem__()
  • iter()
  • list()
  • sorted()

Source: Built-in Functions. Checked 2026-10-15.