This tutorial describes four elegant ways to reverse a list in Python. It includes methods like list.reverse(), built-in reversed() method, slice operator, and by using for loop.
All methods explained here provide ready-to-use code samples. Let’s go over each of them one by one.
List reverse() method
Python list class comes with the default reverse() function that inverts the order of items in the given list. It doesn’t create a new list object instead straightway modifies the original copy.
It is the most recommended way to transpose the list both from speed and performance perspective.
""" Function: Desc: Reverse list using list class built-in reverse() method Param: Input list Return: Modified list object """ def invertList(input_list): input_list.reverse() return input_list # Unit test 1 input_list = [11, 27, -1, -5, 4, 3] print("Input list[id={}] items: {}".format(id(input_list), input_list)) input_list = invertList(input_list) print("Output list[id={}] items: {}".format(id(input_list), input_list))After you run the above code, it outputs the following in the console:
In the above example, you can see that we’ve called the id() function to print the memory address of the list. And you can assert that id values for input and output lists are the same. Hence, we can say that the list. Reverse () method reversed the list in-place and didn’t create a new copy.
Reversed() method
It is Python’s built-in method that reverses all types of sequences such a list, string, tuple, etc. Unlike the first method, neither it performs an in-place reverse operation, nor does it makes a new copy of the list object.
Besides, it returns an iterator pointing to the last element of the list. You can iterate over it and get the items in the reverse order.
""" Function: Desc: Reverse list using Python's built-in reversed() method Param: Input list Return: Reverse iterator """ def invertList(input_list): return reversed(input_list) # Unit test 1 input_list = [11, 27, -1, -5, 4, 3] print("Input list[type={}] items: {}".format(type(input_list), input_list)) result = invertList(input_list) out_list = [item for item in reversed(input_list)] print("Output list[type={}] items: {}".format(type(result), out_list))After you run the above code, it outputs the following in the console:
Input list[type=<class 'list'>] items: [11, 27, -1, -5, 4, 3] Output list[type=<class 'list_reverseiterator'>] items: [3, 4, -5, -1, 27, 11]From the result, you can assess that the type of our input list is a List whereas the one reversed() function returned is of Reverse iterator type.
You may like to use such a method when the list size is big. It would save you some time and memory.
Slice operator to reverse a list
It is an entirely different way to invert the list. No in-place modification occurs, and it makes a new copy of the List. Such a method demands more space to hold a duplicate object and hence, consumes a lot of memory.
""" Function: Desc: Reverse list using Python slice operator Param: Input list Return: New list """ def invertList(input_list): out_list = input_list[::-1] return out_list # Unit test 1 input_list = [11, 27, -1, -5, 4, 3] print("Input list[id={}] items: {}".format(id(input_list), input_list)) out_list = invertList(input_list) print("Output list[id={}] items: {}".format(id(out_list), out_list))After you run the above code, it outputs the following in the console:
Input list[id=139774228206664] items: [11, 27, -1, -5, 4, 3] Output list[id=139774228208840] items: [3, 4, -5, -1, 27, 11]The result shows that the slice operator created a new copy as the id values of both input and out lists are different.
Using for loop to reverse
It is a custom approach to reverse the List using a for loop statement. This approach modifies the original list and also returns a list.
After you run the above code, it outputs the following in the console:
Input list[id=140703238378568] items: [11, 27, -1, -5, 4, 3] Output list[id=140703238378568] items: [3, 4, -5, -1, 27, 11]Using list comprehension
Another customized approach is to use the list comprehension technique to reserve the list. See the below code to understand this method.
""" Function: Desc: Reverse list using list comprehension method Param: Input list Return: Modified list object """ def invertList(input_list): input_list = [input_list[n] for n in range(len(input_list)-1,-1,-1)] return input_list # Unit test 1 input_list = [11, 27, -1, -5, 4, 3] print("Input list[id={}] items: {}".format(id(input_list), input_list)) input_list = invertList(input_list) print("Output list[id={}] items: {}".format(id(input_list), input_list))After you run the above code, it outputs the following in the console:
Input list[id=140044877149256] items: [11, 27, -1, -5, 4, 3] Output list[id=140044877151560] items: [3, 4, -5, -1, 27, 11]