Python reversed() function
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 stringseqString = 'geeks'print(list(reversed(seqString))) # For tupleseqTuple = ('g', 'e', 'e', 'k', 's')print(list(reversed(seqTuple))) # For rangeseqRange = range(1, 5)print(list(reversed(seqRange))) # For listseqList = [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 listdef __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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


