overview of reversed()
The reversed() function returns the reversed iterator object of the given sequence. It is possible to turn the iterator into the original data structure by using the list(), tuple() or a similar function.
use of reversed()
The reversed() function can be used to reverse objects like strings, lists, tuples, ranges, and so on.
# reverse objects using reversed()
myString = 'Python'
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9]
myRange = range(10, 15)
The reversed() function returns the sequences in the reversed order and the list() function is used to turn the iterators into lists.
['n', 'o', 'h', 't', 'y', 'P']
[9, 8, 7, 6, 5, 4, 3, 2, 1]
[14, 13, 12, 11, 10]
syntax of reversed()
The syntax of the reversed() function is:
arguments of reversed()
The reversed() function accepts exactly one argument and it has to be a sequence like a tuple, string, list, or range. An object is supported if it has __len__() and __getitem__() methods.
return values of reversed()
The reversed() function returns an iterator that includes the sequence in the reversed order. The iterator can be turned into the original data structure. For example, if a list was used as an argument, the list() function turns the iterator into a list in reversed order.