Python reversed() function “creates a reversed iterator of a sequence”. It takes a sequence (e.g., list, tuple, or string) as its argument and returns an iterator that yields items from the sequence in reverse order. Since it returns an iterator, you must convert it to the appropriate data type (e.g., list, tuple, or string) to see the reversed items.
Syntax
reversed(seq)
Parameters
The sequence parameter is required, and it is an iterable object.
It could be an object that supports sequence protocol (__len__() and __getitem__() methods) as a tuple, string, list, or range.
Example
# for string
string = 'AppDividend'
print(list(reversed(string)))
# for tuple
tuple = ('k', 'r', 'u', 'n', 'a', 'l')
print(list(reversed(tuple)))
# for range
range = range(19, 24)
print(list(reversed(range)))
# for list
listA = [19, 21, 46, 29, 6]
print(list(reversed(listA)))
Output
In the above code example, we have used the string, range, list, and tuple data types and structures.
That’s it.