Python Tutorial Python Advanced Python Resources Python Libraries
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Python reversed() Function



The Python reversed() method is used to a reverse the order of all elements of an ordered iterable. A ordered iterable can be any data structure like list, tuple, string and range() function etc. It returns the result in a object list which can be accessed using for loop or can be converted into list using list function.

Syntax

reversed(iterable)

Parameters

iterable Required. ordered iterable object like list, tuple, string and range() etc.

Example:

In the example below, reversed() function is used to reverse the order of all elements of a given iterable.

MyList = [1, 2, 3, 4, 5]
print(list(reversed(MyList)))

MyTuple = (10, 20, 30, 40, 50)
print(list(reversed(MyTuple)))

MyString = 'JOHN'
print(list(reversed(MyString)))

MyRange = range(20, 25)
print(list(reversed(MyRange)))

The output of the above code will be:

[5, 4, 3, 2, 1]
[50, 40, 30, 20, 10]
['N', 'H', 'O', 'J']
[24, 23, 22, 21, 20]

❮ Python Built-in Functions

5