Introduction to Python Reverse List
In this article, we are discussing the reverse list in Python. In Python, reverse() is an inbuilt function that is used for reversing the list where this method does not return any value which in turn returns the list with the reverse items in the given list. This function does not create and copy any existing items to the new list but instead, it directly alters the original list of items. There is another way also for reversing a string in Python which is done using slicing and also there is another function is known as reversed() which also works similarly to reverse() function.
Functions of reverse() a list in Python
In this article, we are here to see how to use reverse() built-in function to reverse the given list. This function does not take any arguments and it does not return any value except the reversed elements list.
From the above Syntax the reverse() method reverses the original list. The reversed() function is used to return a reverse iterator.
Examples to Implement of Python Reverse List
Let us see a simple example for a reverse() function which is used for reversing the elements in the given list. This function can be used for reversing the contents of the list object at in-place which means there is no need of creating a new list instead it just modifies the original list.
l = [20, 21, 27, 23, 24, 22] print("The given list is as follows:",l)
print("The reversed list of the given list is as follows:",Reverse(l))
Explanation: In the above program, we can see that there is function reverse() which takes the list to reverse the elements of the given list “l” which has the list [20, 21, 27, 23, 24, 22] and after the function is applied to this list we obtain the reversed list of the given list as [22, 24, 23, 27, 21, 20].
Now let us see an example for another built-in function called reversed() function, this function neither creates any new list nor it reverse the list in-place but we get a reverse iterator instead.
return [ele for ele in reversed(l)] l = [7, 6, 5, 4, 3, 2] print("The given list is as follows:",l)
print("The list after the use of reverses function is as follows:",Reverse(l))
Explanation: In the above program, we can see that we are using reversed() function where we can see we can access the individual elements of the given list using this reversed() function which is used with the “for” loop. So it is better to use reversed() function if we want to access single elements in the given list.
l = [7, 6, 5, 4, 3, 2, 1]
print("The given list is as follows:",l)
for i in reversed(l):
As we discussed earlier reversing a list is not only done by using functions reverse() and reversed() but also we can use slicing for reversing the given list. In Python, slicing is used for reversing the list which this works as to whereas the above functions the copy of the original list is not made but whereas it is done in slicing technique of Python and it also has the list which is not sorted in-place. In this technique, it creates a copy of the list which takes more memory where there is a chance of memory getting exhausted.
new_lst = l[::-1] return new_lst
l = [7, 6, 5, 4, 3, 2, 1] print("The given list to reverse elements using slicing technique:\n",l)
print("The reversed list after using slicing is as follows:")
Explanation: In the above program, we can see that we are using the slicing technique. In this program we can see “new_lst = l[::-1] this statement means that the starting element is nil and the ending element is also nil but the slicing interval is “-1” which means the list is started from backward and hence it will print the given list in the reverse order. So the list “[7, 6, 5, 4, 3, 2, 1]” we have applied slicing to this list so it will result in “[1, 2, 3, 4, 5, 6, 7]”. So we can see it creates another copy of the original list given and due to more comparisons in–place it takes up more memory because it creates a copy that requires more space. Hence slicing technique consumes more space than the above two functions.
In this article, we conclude that to reverse a set of elements or list in Python there are different built-in functions in Python. In Python, reverse() is one of the built-in function for reversing the given list in-place without creating any another list as it does not alter the original list. In Python, another function is used for reversing elements of the list that is named as reversed() this function is used for reversing the list but this will use reverse iterator and the rest is same as reverse() function works. Another and last option for reversing the elements in the list in Python is to use slicing. Slicing is the technique which is used for reversing elements which uses its own syntax but it creates another copy of original list and also consumes more memory.
This is a guide to Python Reverse List. Here we discuss a brief overview on Working of reverse() a list in Python and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –