Python reversed() Function
Python reversed() function returns the reversed iterator of the given sequence.
Signature
Parameters
sequence: It is a sequence that needs to be reversed.
Return
It returns the reversed iterator of the given sequence.
Python reversed() Function Example 1
The above example shows the working of reversed for a sequence: string, tuple, list, and range.
Output:
['a', 'v', 'a', 'J'] ['a', 'v', 'a', 'J'] [11, 10, 9, 8] [5, 7, 2, 1]
Explanation:
In the above example, we have taken a string that consists the value “Java“, and it prints the reversed string value as the output.
In the second case, we have taken a tuple value that consists of the same value, and it prints the reversed tuple value as the output.
In the third case, we have taken a range that consists of a range of numeric values, and it prints the reversed range values as output.
In the fourth case, we have taken a list that consists of some numeric values, and it prints the reversed list value as output.