Inspirational journeys

Follow the stories of academics and their research expeditions

How to Reverse a List in Python Learn Python List Reverse() Method

Mukesh Nirala Sir

Thu, 19 Sep 2024

How to Reverse a List in Python Learn Python List Reverse() Method

Introduction:

  1. As we know that the Python the Reversing a list is a way which takes up a more memory compared to an in-place reversal because it creates a (shallow) copy of the list. 

  2. In general, the reversing a List In-Place With the list. reverse() Method. 

  3. Every list in Python has a built-in reverse() method which is allowed to call to reverse the contents of the list object in-place.

  4. Reversing the list in-place means won't create a new list and copy the existing elements to it in reverse order.

Her I am going to discuss some of the important approach using which the reversal of the list in Python can be get done.

  1. Reversing a list with list.reverse() method 

  2. Using the Slicing Trick to Reverse a Python List, and 

  3. Creating Reverse Iterator with the reversed() Built-In Function

Reversing a list with list.reverse() method:

As we have already discussed earlier in the above segment that Every list in Python has a built-in reverse() method, so if it is needed then we can call this method to reverse the contents of the list object in-place. Reversing a list in-place means it won’t create and copy the existing elements to a new list. Instead, it directly modifies the original list object.

Let us consider the following example as below. Here I am trying to explain the technique in simpler way using the Python console based example.

Example: