Introduction to Python Reverse List
In this article, we are discussing the reverse list in Python. In Python, reverse() is an inbuilt function used for reversing the list where this method does not return any value, which 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; instead, it directly alters the items’ original list. 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 the reverse() function.
Functions of reverse() a list in Python
In this article, we are here to see how to use the 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.
Syntax:
list.reverse();
or
list.reversed();
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 to reverse the contents of the list object at in-place, which means there is no need to create a new list; instead, it just modifies the original list.
Example #1
Code:
def Reverse(l):
l.reverse()
return l
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))
Output:
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].
Example #2
Let us see an example for another built-in function called reversed() function; this function neither creates any new list nor does it reverse the list in-place, but we get a reverse iterator instead.
Code:
def Reverse(l):
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))
Output:
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.
Example #3
Code:
l = [7, 6, 5, 4, 3, 2, 1]
print("The given list is as follows:",l)
for i in reversed(l):
print(i)
Output:
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. This technique creates a copy of the list, which takes more memory where there is a chance of memory getting exhausted.
Example #4
Code:
def Reverse(l):
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:")
print(Reverse(l))
Output:
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 backwards 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.
Conclusion
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 a reverse iterator, and the rest is the same as the reverse() function works. Another and last option for reversing the elements in the list in Python is to use slicing. Slicing is the technique used for reversing elements that uses its own syntax, but it creates another copy of the original list and consumes more memory.
Recommended Articles
We hope that this EDUCBA information on “Python Reverse List” was beneficial to you. You can view EDUCBA’s recommended articles for more information.