In this tutorial, you will learn about Python reversed() with the help of examples.
The reversed() method computes the reverse of a given sequence object and returns it in the form of a list.
Example
seq_string = 'Python'# reverse of a string print(list(reversed(seq_string)))
# Output: ['n', 'o', 'h', 't', 'y', 'P']reversed() Syntax
The syntax of reversed() is:
reversed(sequence_object)reversed() Parameter
The reversed() method takes a single parameter:
- sequence_object - an indexable object to be reversed (can be a tuple, string, list, range, etc.)
Note: Since we can't index objects such as a set and a dictionary, they are not considered sequence objects.
reversed() Return Value
The reversed() method returns:
- a reversed list of items present in a sequence object
Example 1: Python reversed() with Built-In Sequence Objects
seq_tuple = ('P', 'y', 't', 'h', 'o', 'n')# reverse of a tuple object print(list(reversed(seq_tuple)))
seq_range = range(5, 9)# reverse of a range print(list(reversed(seq_range)))
seq_list = [1, 2, 4, 3, 5]# reverse of a list print(list(reversed(seq_list)))
Output
In the above example, we have used the reversed() method with objects like tuple, range and a list.
When using the reversed() method with these objects, we need to use the list() method to convert the output from the reversed() method to a list.
Example 2: reversed() with Custom Objects
class Vowels: vowels = ['a', 'e', 'i', 'o', 'u'] def __reversed__(self): return reversed(self.vowels) v = Vowels()# reverse a custom object v print(list(reversed(v)))
Output
['u', 'o', 'i', 'e', 'a']In the above example, we have used the reversed() method with a custom object v of the Vowels class.
Here, the method returns the reverse order of the sequence in the vowels list.
Recommended Readings:
- Python str()
- Python iter()
Python lists can be reversed using many python method such as using slicingmethod or using reversed() function. This article discusses how both of these work and Which one of them seems to be the faster one and Why.
Code: Reversing a list using Slicing.
Python3
ls = [110, 220, 330,
440, 550]
print('Original list :', ls)
ls = ls[::-1]
print('Reversed list elements :')
for element in ls:
print(element)
Output:
Original list : [110, 220, 330, 440, 550] Reversed list elements : 550 440 330 220 110Explanation : The format[a : b : c] in slicing states that from an inclusive to b exclusive, count in increments of c. In above code, a and b is blank and c is -1. So it iterates the entire list counting from the last element to the first element resulting in a reversed list.
Code: Reversing a list Using reversed() built-in function.
Python3
ls = [110, 220, 330, 440, 550]
print('Original list :', ls)
ls = reversed(ls)
print('Iterator object :', ls)
print('Reversed list elements :')
for element in ls:
print(element)
Output:
Original list : [110, 220, 330, 440, 550] Iterator object : <list_reverseiterator object at 0x7fbd84e0b630> Reversed list elements : 550 440 330 220 110Explanation : The built-in reversed() function in Python returns an iterator object rather than an entire list.
Conclusion :
For a comparatively large list, under time constraints, it seems that the reversed() function performs faster than the slicing method. This is because reversed() just returns an iterator that iterates the original list in reverse order, without copying anything whereas slicing creates an entirely new list, copying every element from the original list. For a list with 106Values, the reversed() performs almost 20,000 better than the slicing method. If there is a need to store the reverse copy of data then slicing can be used but if one only wants to iterate the list in reverse manner, reversed() is definitely the better option.