Python reversed() Function
reversed(seq) returns an iterator that walks a sequence from the last item to the first, without changing the original and without copying it.Use reversed() when you want to look at items back to front: counting down, reading a string backwards, or showing the newest entries first. Because it returns a lazy iterator, wrap it in list(), tuple() or ''.join() when you need a real container.
It works on anything that can be indexed from the end: lists, tuples, strings, ranges and, since Python 3.8, dictionaries. If you only need a reversed copy of a list, slicing with [::-1] is a common alternative.
| Syntax | reversed(seq) |
|---|---|
| Parameters | seq: a sequence (list, tuple, str, range) or an object that defines __reversed__() |
| Returns | A reverse iterator (for a list its type is list_reverseiterator) |
| Changes the original? | No |
| Time complexity | O(1) to create; O(n) to consume all items |
How reversed() works
reversed() does not build a new list. It remembers the sequence and hands out items starting from the last index. Because the result is an iterator, you can loop over it once; after that it is empty.
scores = [70, 85, 92]
back = reversed(scores)
print(list(back))
print(list(back))
print(scores)The second list(back) is empty because the iterator was already used up. The original scores list is untouched.
reversed() vs list.reverse() vs [::-1]
All three give you items in reverse order, but they behave differently. Pick the one that matches what you need to keep.
| Tool | Result | Changes the original? | Works on strings and tuples? |
|---|---|---|---|
reversed(x) | Iterator (lazy) | No | Yes |
x[::-1] | New object of the same type | No | Yes |
x.reverse() | None | Yes, in place | No, lists only |
sorted(x, reverse=True) | New sorted list | No | Yes, but it sorts, it does not just flip |
What reversed() accepts and what it rejects
A sequence is something with a length and numbered positions. Lists, tuples, strings and range objects qualify, and so do dictionaries (they reverse insertion order). A set has no order, and a generator has no way to jump to its end, so both raise TypeError. If you need to reverse such data, first turn it into a list with list() or sorted().
Your own classes can opt in by defining a __reversed__() method, or by providing __len__() and __getitem__(). See classes and objects for how special methods work.
Examples you can run
Press Run, then edit the code and run it again.
Reverse a list without changing it
reversed() gives the items last-to-first; list() collects them.
tasks = ["wake", "code", "sleep"]
for t in reversed(tasks):
print(t)
print(tasks)Expected outputsleep
code
wake
['wake', 'code', 'sleep']
Reverse a string and check for a palindrome
A string is a sequence of characters, so ''.join(reversed(text)) rebuilds it backwards.
word = "level"
back = "".join(reversed(word))
print(back)
print(word == back)Expected outputlevel
True
Count down with range
reversed(range(...)) counts down without computing a negative step.
for n in reversed(range(1, 4)):
print(n)
print("liftoff")Expected output3
2
1
liftoff
Reverse a dictionary's insertion order
Since Python 3.8 dictionaries can be reversed. You get the keys, newest first.
orders = {"A1": 10, "A2": 25, "A3": 5}
print(list(reversed(orders)))Expected output['A3', 'A2', 'A1']
Common mistakes
Printing the iterator instead of the items
✗ Wrong
nums = [1, 2, 3]
print(reversed(nums))✓ Right
nums = [1, 2, 3]
print(list(reversed(nums)))reversed() returns an iterator, not a list. Wrap it in list() to see the values.
Indexing the result
✗ Wrong
nums = [1, 2, 3]
r = reversed(nums)
print(r[0])✓ Right
nums = [1, 2, 3]
r = list(reversed(nums))
print(r[0])An iterator cannot be indexed. Convert it to a list first, or use nums[-1] for the last item.
Reversing a set
✗ Wrong
tags = {"a", "b"}
reversed(tags)✓ Right
tags = {"a", "b"}
print(list(reversed(sorted(tags))))Sets have no order. Convert to a sorted list first, then reverse.
Python reversed() Function: questions people ask
Does reversed() change the original list?
No. reversed() leaves the original untouched and returns a new iterator. To reverse a list in place use list.reverse().
How do I reverse a string in Python?
Use ''.join(reversed(text)) or the slice text[::-1]. Both return a new string because strings are immutable.
What does reversed() return in Python 3?
It returns an iterator object, not a list. Use list(reversed(x)) when you need the items in a list.
Keep learning
Step-by-step lessons:
Python ListsPython SlicingPython Iterators
Related:
Python enumerate() FunctionPython sorted() FunctionPython range() FunctionPython List Methods
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →