reversed() method returns an iterator that accesses the given sequence in the reverse order.
Syntax :
reversed(sequ)
Parameters :
sequ : Sequence to be reversed.
Returns :
returns an iterator that accesses the given sequence in the reverse order.
CODE 1
# Python code to demonstrate working of # reversed() # For string seqString = 'geeks'print(list(reversed(seqString))) # For tuple seqTuple = ('g', 'e', 'e', 'k', 's') print(list(reversed(seqTuple))) # For range seqRange = range(1, 5) print(list(reversed(seqRange))) # For list seqList = [1, 2, 4, 3, 5] print(list(reversed(seqList))) |
Output :
['s', 'k', 'e', 'e', 'g'] ['s', 'k', 'e', 'e', 'g'] [4, 3, 2, 1] [5, 3, 4, 2, 1]
CODE 2
vowels = ['a', 'e', 'i', 'o', 'u'] # Function to reverse the list def __reversed__(self): return reversed(self.vowels) # Main Function if __name__ == '__main__': print(list(reversed(vowels))) |
Output :
['u', 'o', 'i', 'e', 'a']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Reversed Split Strings
- Python | Reversed Order keys in dictionary
- Python - reversed() VS [::-1] , Which one is faster?
- Python - K elements Reversed Slice
- Python - Assign Reversed Values in Dictionary
- Python - Call function from another function
- Wand function() function in Python
- Returning a function from a function - Python
- wxPython - GetField() function function in wx.StatusBar
- How to write an empty function in Python - pass statement?
- Function Decorators in Python | Set 1 (Introduction)
- Vulnerability in input() function – Python 2.x
- Function Annotations in Python
- Sorted() function in Python
- Ways to sort list of dictionaries by values in Python - Using lambda function
- Python Numbers | choice() function
- Python | askopenfile() function in Tkinter
- Python | Binding function in Tkinter
- floor() and ceil() function Python
- Sum 2D array in Python using map() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


