Python reversed() Method
Last Updated :
02 Jul, 2024
Python reversed() method returns an iterator that accesses the given sequence in the reverse order.
Example:
Python
# creating a list
cars = ["nano", "swift", "bolero", "BMW"]
# reversing the list
reversed_cars = list(reversed(cars))
#printing the list
print(reversed_cars)
Output['BMW', 'bolero', 'swift', 'nano']
Python reversed() Method Syntax
reversed(sequence)
Parameter :
- sequence : Sequence to be reversed.
Return : Returns an iterator that accesses the given sequence in the reverse order.
How to use reversed function in Python?
reversed() method is very easy to use and it returns an iterator that accesses the list in reverse order. Let’s understand it better with an example.
Example:
In the given example, we are reversing elements of the list with reversed() function in Python.
Python
my_list = ["apple", "banana", "cherry", "date"]
reversed_list = list(reversed(my_list))
print(reversed_list)
Output['date', 'cherry', 'banana', 'apple']
More Python reversed() Method Examples
Let’s see some of the other common scenarios for reversed() method.
1. 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.
Python
# 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]
2. Python reversed() with for loop in Python
In this example, we are using reversed() function to show it’s working with Python loops.
Python
# 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
3. Python 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.
Python
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']
4. Python reversed() method with List
In this example, we are reversing a list of vowels with the reversed function in Python.
Python
vowels = ['a', 'e', 'i', 'o', 'u']
print(list(reversed(vowels)))
Output['u', 'o', 'i', 'e', 'a']
5. Python reversed() method with string
In this example, we are reversing a string with the reversed function in Python.
Python
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.
Python
# 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)We have covered the definition, syntax and different uses of reversed() method in Python. Python reversed() function is very important function to access sequence from the end.
reversed() method can be used to reverse a set, list,tuple, etc in Python.
Python reversed() Method – FAQs
Can you provide Example of Using reversed() with a List?
You can use the reversed() function to reverse the elements of a list. Here’s an example:
original_list = [1, 2, 3, 4, 5]
reversed_list = list(reversed(original_list))
print(reversed_list) # Output: [5, 4, 3, 2, 1]
How Does reversed() Differ from the reverse() Method?
reversed():- Returns an iterator that accesses the given sequence in reverse order.
- Does not modify the original sequence.
original_list = [1, 2, 3, 4, 5]
reversed_iterator = reversed(original_list)
reversed_list = list(reversed_iterator)
print(original_list) # Output: [1, 2, 3, 4, 5]
print(reversed_list) # Output: [5, 4, 3, 2, 1]
reverse():- Modifies the original list to reverse its elements.
original_list = [1, 2, 3, 4, 5]
original_list.reverse()
print(original_list) # Output: [5, 4, 3, 2, 1]
What Data Types Can We Apply the reversed() Method To?
The reversed() function can be applied to:
- Lists
- Tuples
- Strings
- Range objects
It returns an iterator that accesses the sequence in reverse order.
How to Reverse a String Using reversed()
You can reverse a string by using reversed() and then joining the characters back together:
original_string = "Hello, world!"
reversed_string = ''.join(reversed(original_string))
print(reversed_string) # Output: "!dlrow ,olleH"
Can We Use reversed() with Tuples and Sets?
original_tuple = (1, 2, 3, 4, 5)
reversed_tuple = tuple(reversed(original_tuple))
print(reversed_tuple) # Output: (5, 4, 3, 2, 1)
- Tuples: Yes, you can use
reversed() with tuples, and it will return an iterator. - Sets: No, you cannot use
reversed() with sets because sets are unordered collections and do not maintain any specific order.
Please Login to comment...