Learn and Learn

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

Python – Reverse Order of Items in List – Examples & Explanation

There are different ways to reverse the order of the list in Python. To reverse the order of the list in Python you may use the following methods.

  • reversed() global function of Python.
  • reverse() method of Python list class.
  • list slicing to reverse the order of the list in Python

It should be noted that reverse() function performs an action on actual list object and doesn’t return any value or list object. It points to list object only. You can understand this behavior from the following example.

 

How to Reverse the Order of the list in Python Using the reverse() Function


Output:

Reversed List (before conversion): <list_reverseiterator object at 0x0069A3F0>
Reversed List (after conversion): [5, 4, 3, 2, 1]

 

How to Get Individual list Items/Elements in Reverse Order

By using for loop we reverse the items of the list and print it outside the list. Look at the following example. This way you can get individual list items in reverse order.


Output:
5
4
3
2
1

 

Using the reverse() method of list in Python to Reverse the Order of the list

You can also use the reverse() method of list class to achieve the same results.


This should be noted that MyList.reverse() reverses the actual list MyList object.
Output:
[5, 4, 3, 2, 1]

 

How to Reverse the Order of the list Items/Elements using the list slicing


Output:
This should be noted that MyList[::-1] reverses the actual list MyList object.
[5, 4, 3, 2, 1]

Conclusion – Reverse list in Python:

Extended slice syntax of Python iterator or sequence is the most preferable method to reverse a list. Although, It is the fastest method to reverse the list or any knind of iterator in Python and most of the programmers use this method. It is not an easy to understand for some beginner level programmer. list.reverse() is clear and readable for all level of programmers but it comes in computational cost. If your iterator or sequence is small in size then you may not notice any difference. It all depends on personal choice but it should follow the standards.

 

LearnAndLearn.com © 2018 - All rights reserved