Python reversed() function
Loop over a sequence backwards.
Syntax
reversed(seq)
Returns an iterator, not a list: wrap it in list() to see it. For strings, s[::-1] is the usual way to reverse.
Parameters
| seq | a list, tuple, string or range |
Return value
an iterator
Example
Code
for n in reversed([1, 2, 3]): print(n) print(list(reversed("abc"))) print("abc"[::-1])
Output
3 2 1 ['c', 'b', 'a'] cba
Related
Learn it step by step in the free lesson Lists & Tuples, with exercises that check your answers.