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

Related Courses

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:

>>> mylist = [1, 2, 3, 4, 5]
>>> mylist[1, 2, 3, 4, 5]
>>> mylist.reverse()
None
>>> mylist
[5, 4, 3, 2, 1]

Note:

  1. From the above example it is clear that when we are calling reverse() method then it returned ‘None’ but, modified the original list object. 

  2. When we are using the reverse() method then it is basically used to modifies the sequence of the element present in the list. 

  3. But it should be get noted that it does not return a reversed list values as a output but when we are going to print or display the list then it get printed out.

Using the Slicing Trick to Reverse a Python List:

Slicing in Python is another feature using which we can reverse the element of the list. If we need to apply this technique, then we have to use the following technique to make it possible. 

Let us consider the following example such as

 reverse_list(list):
    reversed_list = list[::-1]
    >>> mylist=[1, 2, 3, 4, 5]
    >>> mylist[::-1]
   [5, 4, 3, 2, 1]

Note:

  1. Here when we are going to use this technique then the structure of the list is get replicated but the elements are not. Hence, the elements are not duplicated thus saving space. 

  2. As you know that the list in Python are mutable so when we are going to apply this technique to the elements contained in a list then the object is get modified. Since the objects are get modified so that will be get reflected in other copies as well.

  3. The Slicing technique is fast and reliable for the developer. But sometimes it becomes difficult to understand when the decreasing readability of code is supposed to be get done. 

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

It is another important technique in Python using which we can reverse the list element. When we are going to use the reversed() function then it will return an iterator using which if we need then we can be able to access elements in reverse order. 

But it should be get noted that If we are going to access a single individual element of the list in reverse order you can use this function. It does not reverse a list in-place neither it creates a copy.

Let us consider the following example as below.

>>> mylist = [1, 2, 3, 4, 5]
>>> for item in reversed(mylist):
...     print(item)
5
4
3
2
1
>>> mylist
>>> [1, 2, 3, 4, 5]

Note:

  1. In the above example it is clear that when we are going to use this function then it returns element of the list in reverse order using iterator pattern.

  2. Another way of getting a reverse list using list constructor.

To explain the concept of list constructor let us consider the following example as below.

>>> mylist = [1, 2, 3, 4, 5]
>>> list(reversed(mylist))
[5, 4, 3, 2, 1]
  1. Here in the above program I’m calling the list() constructor on the result of the reversed() function?.

  2. When we are using the list constructor it is used to built-in keeps iterating until the (reverse) iterator is exhausted.

  3. It is basically used to keep and puts all the elements which is get fetched from the iterator during the execution and puts into a new list object.

  4. And as a result a reversed shallow copy of the original list is get reformed and displayed.

  5. It is another convenient approach to reverse the list element in Python.

Scope @ NareshIT:

  1. At Naresh IT you will get a good Experienced faculty who will guide you, mentor you and nurture you to achieve your dream goal.

  2. Here you will get a good hand on practice in terms of practical industry-oriented environment which will definitely help you a lot to shape your future.

  3. During the designing process of application, we will let you know about the other aspect of the application too. 

  4. Our Expert trainer will let you know about every in’s and out’s about the problem scenario.

Achieving your dream goal is our motto. Our excellent team is working restlessly for our students to click their target. So, believe on us and our advice, and we assured you about your sure success.