Python 3 – List reverse() Method
In Python, lists are mutable ordered collections of items. They are one of the most commonly used data structures in Python. The reverse() method in Python is used to reverse the order of the elements in a list. This method is used to modify the original list and does not create a new list. In this article, we will cover the reverse() method in detail, its syntax, and its usage with examples.
Syntax of the reverse() Method
The syntax of the reverse() method is as follows:
list.reverse()
Here, list is the name of the list on which we want to apply the reverse() method.
Parameters of the reverse() Method
The reverse() method does not accept any parameters.
Return Value of the reverse() Method
The reverse() method does not return any value. It simply reverses the order of the elements in the list.
Example of the reverse() Method
Let’s start with a simple example of the reverse() method to understand its usage.
# Example 1 - Simple Usage
numbers = [1, 2, 3, 4, 5]
numbers.reverse()
print(numbers)
Output:
[5, 4, 3, 2, 1]
In the above example, we have a list named numbers that contains integers from 1 to 5. We have applied the reverse() method on this list, which reverses the order of the elements in the list. The modified list is then printed using the print() function.
Reversing a String
In Python, strings are also considered as a sequence of characters. Therefore, we can also use the reverse() method to reverse the order of characters in a string. Let’s see an example to understand this concept.
# Example 2 - Reversing a String
string = "Hello, World!"
string_as_list = list(string)
string_as_list.reverse()
reversed_string = "".join(string_as_list)
print(reversed_string)
Output:
!dlroW ,olleH
In the above example, we have a string named string that contains the value “Hello, World!”. We have converted this string into a list using the list() method. Then, we have applied the reverse() method on this list, which reverses the order of characters in the list. Finally, we have joined the elements of the list using the join() method and assigned the resultant string to the variable reversed_string. The final output is the reversed string, which is printed using the print() function.
Reversing a List using Slicing
Python also provides an alternate way to reverse a list using slicing. In this approach, we create a copy of the original list using the slicing operator [:], and then we reverse the copy using the step value of -1. Let’s see an example to understand this concept.
# Example 3 - Reversing a List using Slicing
numbers = [1, 2, 3, 4, 5]
reversed_numbers = numbers[::-1]
print(reversed_numbers)
Output:
[5, 4, 3, 2, 1]
In the above example, we have a list named numbers that contains integers from 1 to 5. We have created a copy of the original list using the slicing operator [:] and then we have reversed the copy using the step value of -1. Finally, we have printed the reversed list using the print() function.
Conclusion
In this article, we have learned about the reverse() method in Python. We have seen its syntax, its usage, and its example with lists, strings, and the slicing operator. The reverse() method is a useful method to reverse the order of elements in a list, and it can also be used to reverse the order of characters in a string.