Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Python – Reverse a List – reverse() Function with Examples

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:

 

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.

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).

Output:
['orange', 'blue', 'green', 'yellow', 'red']

LearnAndLearn.com © 2018 - All rights reserved