reverse()
function is used to reverse the order of elements in a list. reverse()
function is very helpful for managing lists. There are some other methods to reverse the list order by using slicing operator or reversed()
method. But reverse()
function is one of the easiest and simple one.
Syntax of reverse()
Function
The syntax of reverse()
function in Python is:
list.reverse()
Parameters of reverse()
Function in Python
We did not pass any argument to the
reverse()
function.
Compatibility
reverse()
function is available and compatible with both Python 2.x
and 3.x
. This function is one of python list methods.
Return Value of reverse()
Function in Python
reverse()
function didn’t return anything.
Python reverse()
Function Example 1
There is a list of some character (a, b, c, d
) and the list can be converted into (d, c, b, a
) by using reverse()
function.
# a character list characters = ['a', 'b', 'c', 'd'] # list going to be reversed characters.reverse() #Printing the reversed list print(characters)
Output:
['d', 'c', 'b', 'a']
Python reverse()
Function Example 2
There is a list of some colors (red, yellow, green, blue, orange
) and after reversing the list will be shown as (orange, blue, green, yellow red
).
# a colors list colors = ['red', 'yellow', 'green', 'blue', 'orange'] # list going to be reversed colors.reverse() #Printing the reversed list print(colors)
Output:
['orange', 'blue', 'green', 'yellow', 'red']