reversed
Iterate over the items of a sequence in reverse order.
Python 3.13
reversed(seq)Parameters
- seqReversible
- The object to traverse from the end.
Return value
An iterator that can be traversed only once; wrap it in list() to get a list.
Examples
String characters in reverse order
Python 3.13
print(list(reversed('hello')))['o', 'l', 'l', 'e', 'h']
Looping from the end
Python 3.13
for number in reversed(range(1, 4)):
print(number)3 2 1
Details
The original object is not changed. A dictionary is traversed by its keys from the end, while sets and iterators are not supported and raise TypeError.
