The Wayback Machine - https://web.archive.org/web/20231119131725/https://www.geeksforgeeks.org/python-reversed-function/
Open In App
Related Articles

Python reversed() function

Improve Article
Improve
Save Article
Save
Like Article
Like

Python reversed() method is a built-in method that returns an iterator that accesses the given sequence in the reverse order.

Syntax of Python reversed() function

Syntax: reversed(sequence) 

Parameter :

  • sequ : Sequence to be reversed. 

Return : Returns an iterator that accesses the given sequence in the reverse order. 

Example

In the given example, we are reversing elements of the list with reversed() in Python.

Python3




my_list = ["apple", "banana", "cherry", "date"]
reversed_list = list(reversed(my_list))
print(reversed_list)


Output

['date', 'cherry', 'banana', 'apple']


Python reversed() Function Examples

Python reversed() with Built-In Sequence Objects

In the given example we have used reversed() with tuple and range. When using reversed with these objects we need to use the list() method to convert the output from reversed() to list.

Python3




# For tuple
seqTuple = ('g', 'e', 'e', 'k', 's')
print(list(reversed(seqTuple)))
 
# For range
seqRange = range(1, 5)
print(list(reversed(seqRange)))


Output

['s', 'k', 'e', 'e', 'g']
[4, 3, 2, 1]


reversed() with for loop in Python

In this example, we are using reversed() function to show it’s working with Python loops.

Python3




# Create a string
str = "Reversed in Python"
 
# Reverse the string and print its characters in reverse order
for char in reversed(str):
    print(char, end="")


Output

nohtyP ni desreveR

reversed() in Python with custom objects

In this example, we are creating a class gfg which includes a list of vowels we are using the reversed function to reverse the vowels.

Python3




class gfg:
    vowels = ['a', 'e', 'i', 'o', 'u']
 
    # Function to reverse the list
    def __reversed__(self):
        return reversed(self.vowels)
 
# Main Function   
if __name__ == '__main__':
    obj = gfg()
    print(list(reversed(obj)))


Output

['u', 'o', 'i', 'e', 'a']


Python reversed() with List

In this example, we are reversing a list of vowels with the reversed function in Python.

Python3




vowels = ['a', 'e', 'i', 'o', 'u']
print(list(reversed(vowels)))


Output

['u', 'o', 'i', 'e', 'a']


Python reversed() with string

In this example, we are reversing a string with the reversed function in Python.

Python3




str = "Geeksforgeeks"
print(list(reversed(str)))


Output

['s', 'k', 'e', 'e', 'g', 'r', 'o', 'f', 's', 'k', 'e', 'e', 'G']


Exception in reversed() function

In this example, we are showing the exception in reversed() function.

Python3




# Create a list
lst = [1, 2, 3]
 
# Reverse the list using the `reversed` function
reversed_lst = reversed(lst)
 
# Print the reversed elements one by one using the `next` function
print(next(reversed_lst)) 
print(next(reversed_lst)) 
print(next(reversed_lst)) 
 
# Attempting to print the next element will raise a StopIteration exception
print(next(reversed_lst))  # Exception


Output

3
2
1
StopIteration
    print(next(my_list_rev))  # Exception
Line 13 in <module> (Solution.py)


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 17 Nov, 2023
Like Article
Save Article
Similar Reads
Related Tutorials