In this article, we are going to learn all about the Python reversed() function. The reversed function is a built-in function in python that returns reversed version of objects. You can explore more Python built-in functions by clicking here.
Headings of Contents
Python reversed() function Introduction
Python reversed() is a built-in function that comes with Python by default. The reversed() function is used to return reversed version of the iterable (i.e. string, list, tuple, set, etc).
Syntax
The syntax of reversed function in Python is:-
reversed(sequence)
Parameter
reversed function in Python accepts one parameter sequence.
- sequence:- sequence can be iterable.
Return Value
The return value of the reversed() function in Python is reversed object.
Python reversed function example
Here we will understand Python reversed() function along with different-2 examples so that you can understand all about Python reversed() function.
Example: Reverse list using reversed() function
my_list = ['Python', 'Java', 'C++', 'C', 'Ruby']
print(list(reversed(my_tuple)))
Output
['Ruby', 'C', 'C++', 'Java', 'Python']
Example: Reverse tuple using reversed() function
my_tuple = (1, 2, 3, 4, 5, 6, 7)
print(list(reversed(my_list)))
Output
[7, 6, 5, 4, 3, 2, 1]
reversed() function with for loop
Python reversed() function can also use with Python for loop.
Example: Using reversed() function with for loop
my_list = ['Python', 'Java', 'C++', 'C', 'Ruby']
for i in reversed(my_list):
print(i)
Output
Ruby
C
C++
Java
Python
Conclusion
In this tutorial, you have seen all about Python reversed() function along with examples. reversed function in Python is the best option to reverse any iterable object easily.
If you like this article, please share and keep visiting for further python tutorials.
Python built-in functions
For more information:- Click Here
Thanks for your valuable time … 🙏🙏❤️❤️