Fastest way to reverse a list in python

Learn how to reverse a list in Python.

Abstract

In Python, a list is an important and widely used data type. Multiple values can be stored in the list (that can be heterogenous). Lists are mutable (inside values can be changed) and ordered(means the elements will remain in same order as you have specified during declaration of list, until and unless you change that order) in nature. As a result of having so many features, we can also use reverse operation on the list. Reversing the list here refers to directly modify the original list elements rather than creating a new list and copy the elements there in reverse order.

Scope Of Article

  • This article will show you how to reverse a list in Python using three different methods, which are listed below.
    1. reverse() method
    2. reversed() method
    3. slicing method
  • In this article every approach for reversing a list is explained with an example.

Introduction

While solving some big problems one may need to reverse the list. As a programmer, while studying python basics one can notice that reversing a string in python can help you access required data more quickly rather than taking traversing the list in normal order first and then in reversed order.

Let's say you've entered a string, such as your name, and you want to print the alphabets in that string in reverse order. In this case, reversing a list in Python is required.

Fastest way to reverse a list in python

And we all know how easy it is to use Python's built-in functions and data structures, such as list(data structure), which allows you to store and manipulate various data types within the list.

So now, we'll look at how to reverse a list in Python in the next section of this article.

Syntax

In Python, there is a built-in function called reverse() that is used to reverse the list. This is a simple and quick way to reverse a list that requires little memory. Syntax- list_name.reverse() Here, list_name means you have to write the name of the list which has to be reversed.

Note:- In this reverse() function, it doesn’t return any value, it directly modifies the original list in-place means without creating a new list.

This function returns the None value as it modifies the original list directly. Let’s understand this with an example:

    Test_list=[1,2,3,4,5]
    Test_list.reverse()
    print(Test_list) # Output:  [5,4,3,2,1]

    New_list=Test_list.reverse()
    print(New_list) # Output: None

As shown in the script above it is clear that the reverse() function directly modifies the original list in-place instead of returning a value.

There are pros and cons of this function:-

Pros- Extra Space is not required for storing the reversed list because of its in-place reversing feature.

Cons- This function just overwrites the original list itself so the original list can’t be accessed( can only be retrieved after again reversing the string).

But the list can be copied before reversing the list in the new list so as to save the original list. This is done using the copy() function.

Let’s take an example:

     Test_list=[1,2,3,4,5]
     New_list=Test_list.copy()
     New_list.reverse()

     print(“Original List: ”,Test_list) # Output: [1,2,3,4,5]
     print(“Reversed List: ”,New_list) # Output: [5,4,3,2,1]

As in the above example, there is a list that is "Test_list", now we have to reverse it. So we will no directly reverse the original list, we will firstly copy that list in another list that is "New_list"(using the copy() function) and then apply the reverse() function on it. So when we will print that list(New_list), it will print reversed list as an output.

Parameter

This reverse() function doesn’t need any parameter, it just modifies the original list. We just have to put the name of the list(that has to be reversed) along with the dot(.) then use this reverse() function.

As an example you can refer to the previous one, where it is observable that reverse() is used there which is not taking any parameter with it.

So it is cleared that this reverse() function doesn’t need any kind of parameter.

Return Value

The reverse() function doesn’t return any value. This is because it directly modifies the original list in-place without creating a new list or space.

Let’s take an example:

    test_list=[1,2,3]
    x=test_list.reverse()

    print(x) # Output: None
    print(test_list) # Output: [3, 2, 1]

As in the above script, we have stored the reversed value in the “x” but as we know that it returns None, so when the “x” is printed, None comes out as an output. Whereas when “test_list” is printed, the reversed list comes as an output that has to be.

1st Method- Reverse a list using list.reverse() method

This list.reverse() approach in python allows you to reverse a list in place. It makes in-place changes to the original list, so no extra memory is needed. The disadvantage is that the previous(original) list is changed.

In Python, there is a built-in function called reverse() that is used to revers the list. This is a simple and quick way to reverse a list that requires little memory.

Syntax- list_name.reverse() Here, list_name means you have to write the name of the list which have to be reversed.

Let’s take an example:

     Test_list=[1,2,3,4,5]
     New_list=Test_list.copy()
     New_list.reverse()

     print(“Original List: ”,Test_list) # Output: [1,2,3,4,5]
     print(“Reversed List: ”,New_list) # Output: [5,4,3,2,1]

As in the above example there is a list that is "Test_list", now we have to reverse it. So we will no directly reverse the original list, we will firstly copy that list in another list that is "New_list"(using the copy() function) and then apply the reverse() function on it. So when we will print that list(New_list), it will print reversed list as an output.

This method is quick, straightforward, and self-explanatory. This would be the option I like to use whenever I need to reverse a list in-place and don't want to create an extra copy but don't mind modifying the original.

2nd Method- Reverse a list using reversed() built-in function

This reversed() in-built function return only the iterator which is used to access the elements in the reversed order. In this function, no new list is created, and no original list is modified. This function takes the list as a parameter that has to be reversed.

The iterator just iterates the whole given list in the reversed order so that the elements can be printed in that order or can be stored in the new list so as to print it later.

Syntax- reversed(list_name) Here, list_name means you have to write the name of the list which have to be reversed.

Let’s take an example:


    test_list=[1,2,3,4,5]
    for ele in reversed(test_list):
            print(ele)


    Output: 5
            4
            3
            2
            1

As in the above example there is a list named as "test_list" and we have used the reversed() function inside a 'for' loop which helps to iterate over all the list elements in reverse order(iterator starts from the last elements and end it up to the first one).

But the problem here is that the elements here are printing separately and what if we want them all in the list in reversed order, so for that we will call the list() constructor over the result of the reversed() function.

    test_list=[1,2,3,4,5]
    print(list(reversed(test_list)))  # Output: [5,4,3,2,1]

Elements can also be appended in the new list while iterating and then print that new list at last. Let’s have an example:

    test_list=[1,2,3,4,5]
    new_list=[ ]
    for ele in reversed(test_list):
         new_list.append(ele)

    print(new_list)  # Output: [5,4,3,2,1]

Advantages:

  1. Returns an iterator which iterates the list in reverse order.
  2. Doesn’t need to modify the original list.

3rd Method- Reverse a list using the slicing technique

As python comes up with an impressive feature that is list slicing. List slicing works with the syntax given below.

Syntax- list_name[start:stop]. Here, list_name means you have to write the name of the list which have to be reversed. Start parameter indicates the value from where the user wants to slice the list or iterate it. Stop parameter indicates the value till where the user wants to slice the list or iterate it. Step parameter indicates the value with which user wants to skip the elements between the start and stop index of the list.

Let’s take an example:

  • [ : : ] If you will leave those parameters empty then it creates a copy of the list. Let’s take an example:

       test_list=[1,2,3,4]
       print(test_list[ : :]) #Output : [1,2,3,4]

As you can see from the above script that start, stop, step are passed as a paramter without any values inside them which indicates that the list will iterate normally from 0th index to the nth index with 1 as a step by default.

  • [: : n] If you fill will out the ‘step’ parameter only then it will create a copy but that list(copy) will have every next nth element of the original list. Let’s take an example:

        test_list=[1,2,3,4,5,6,7]
        print(test_list[ : :2]) # Output: [1,3,5,7]

As you can see from the above script that start and stop are passed as a parameter without any values inside them but step is passed with value '2' which means that this function will print the list from starting to the end with every 2nd element.

  • [ : : -1] If you will pass ‘-1’ as a step parameter only then it will print the list in reverse order. So we can use this to print the reversed list as given below. Let’s take an example:

        test_list=[1,2,3,4,5] 
        print(test_list[ : :-1]) # Output: [5,4,3,2,1]

As you can see from the above script that start and stop are passed as a parameter without any values inside them but step is passed with value '-1' which means that iterator will start iterating from the last element(5) and iterate till first element(1).

From this approach, we are iterating all the elements in the list but in the reverse order that is from stop to start.

But this approach has the drawback that it creates a reversed copy of the list which takes the memory.

Advantages

  1. Doesn’t modify the original list.

Conclusion

As there are numerous approaches listed above, one can select any of them based on their requirements. Each method is superior in its own right. There have been many other approaches in the past, and there will be many more in the future.

Read More:

  1. String Slicing in Python
  2. Reverse a Number in Python
  3. String reverse in Python

What is the easiest way to reverse a list in Python?

In Python, there is a built-in function called reverse() that is used to revers the list. This is a simple and quick way to reverse a list that requires little memory. Syntax- list_name. reverse() Here, list_name means you have to write the name of the list which have to be reversed.

How do you reverse a list without reverse method in Python?

In order to reverse a list without using the built-in reverse() function, we use the Slicing Operator. The slicing operator is another method used for reversing the data elements.

How do you reverse a list without using slicing?

Another way to reverse python list without the use of any build-in methods is using loops. Create an empty list to copy the reversed elements. In the for loop, add the iterator as a list element at the beginning with the new list elements. So in that way, the list elements will be reversed.

How do you reverse all elements in a list Python?

Python lists can be reversed in-place with the list. reverse() method. This is a great option to reverse the order of a list (or any mutable sequence) in Python. It modifies the original container in-place which means no additional memory is required.