reversed()

Return a lazy reverse iterator over any sequence — list, tuple, string, range, or anything with __reversed__.

Built-in functionPython 2.4+Live demo
Common call
for x in reversed(items):
Returns
iterator — wrap in list() to materialize
Replaces
the [::-1] slice, which builds a full reversed copy
Watch out
does NOT work on sets, dicts, or generic iterables
reversed(seqseqAnything supporting reverse iteration: list, tuple, string, range, bytes, or a type with __reversed__ (or __len__ + __getitem__). Sets and generic generators are refused.type: sequence · required)
reversed

Demo

Live evaluation
Try:
Inputs
itemslistcomma-separated items
Output
reversed(['a', 'b', 'c', 'd'])
['d', 'c', 'b', 'a']

reversed returns an iterator — the demo materializes it as a list so you can see the whole result. In real code you iterate directly: `for x in reversed(items):`. The source sequence is NOT copied — reversed just walks it back-to-front.

Parameters

NameTypeRequiredDescription
seqsequenceyesAnything supporting reverse iteration: list, tuple, string, range, bytes, or a type with __reversed__ (or __len__ + __getitem__). Sets and generic generators are refused.

Return value

reversedAn iterator that yields the items of seq in reverse order. Lazy — nothing is copied.

Common patterns

Iterate backwards
The clean replacement for range(len(items) - 1, -1, -1) indexing gymnastics.
for item in reversed(items):
    process(item)
Enumerate from the end
Pair reversed with enumerate — but note: enumerate still counts forward.
for i, item in enumerate(reversed(items)):
    ...
Reverse a string without copying twice
reversed(s) is a lazy iterator; join materializes once at the end.
flipped = "".join(reversed(s))
Bottom-up processing
Iterating a sorted list in reverse is common for "largest first" workflows.
for score, name in reversed(sorted(scores)):
    print(name, score)

Examples

1. Basic list
list(reversed([1, 2, 3]))
Returns
[3, 2, 1]
2. String
list(reversed("abc"))
Returns
["c", "b", "a"]
3. Range
list(reversed(range(3)))
Returns
[2, 1, 0]
4. Empty is empty
list(reversed([]))
Returns
[]
5. Fluent join
"".join(reversed("hello"))
Returns
"olleh"

Pitfalls

1. reversed() does NOT accept sets or general iterables
Sets are unordered — there is nothing to reverse. Generic iterables (generators, map/filter objects) do not support __reversed__. Convert to a list first.
Type error
list(reversed({1, 2, 3}))
TypeError: argument to reversed() must be a sequence
Materialize first
list(reversed(list({1, 2, 3})))
reversed of the list snapshot
2. Iterator exhausts after one pass
reversed returns an iterator, not a list. Loop through it twice and the second loop sees nothing.
Empty on reuse
r = reversed([1, 2, 3])
list(r)   # [3, 2, 1]
list(r)   # []
second list() is empty
Materialize once
r = list(reversed([1, 2, 3]))
# reuse r freely
reusable list
3. Confused with list.reverse
reversed(xs) returns a NEW iterator and leaves xs alone. xs.reverse() MUTATES xs and returns None. Reaching for the wrong one silently changes (or fails to change) your list.
No effect on source
xs = [1, 2, 3]
reversed(xs)
xs
[1, 2, 3] # reversed() alone does nothing
Pick your intent
xs.reverse()          # mutate
# or
ys = list(reversed(xs)) # new list
explicit choice
4. [::-1] is often the right tool instead
reversed is lazy but returns an iterator. `xs[::-1]` returns a fully-materialized reversed copy. Pick based on whether you need laziness or a real sequence.
When you need a list
reversed(xs)  # object, not indexable
<list_reverseiterator object>
Slice returns a list
xs[::-1]
[..., 3, 2, 1] # ordinary list

When to use

Use it
  • Iterating backwards without allocating a copy
  • Pipelines feeding another iterator (join, sum, next)
  • Very large sequences where a copy would hurt
  • Reading log lines from the end
Reach for something else
  • Sets, dicts, generators → convert to list first, or reach for a different tool
  • Indexing / slicing the result → use xs[::-1] which returns a real sequence
  • Reordering in place → list.reverse

Notes

Complexity
O(1) to construct; O(n) to iterate
Return
reversed iterator — one-shot
CPython impl
Objects/enumobject.c :: reversed_iterator — walks the source by index in reverse
Memory
O(1) — no copy of the source
Thread-safe
The iterator is safe; the source should not mutate during iteration

FAQ

reversed(xs) is lazy — an iterator that yields items on demand, no copy. xs[::-1] builds a fully-materialized reversed copy. Use reversed when you feed the result into another iterator; use the slice when you need to keep or index it.

History

2.4
reversed() introduced.
3.8
dicts became reversible — reversed(d) yields keys in reverse insertion order.