Python List reverse
At times, when we are required to reverse our list, we can do it with the help of the reverse method. As the name suggests, the reverse method is going to reverse the list for us. This method does not return anything, and it does not take any arguments. It just updates the current list. Let’s have a look at a simple program, which demonstrates to us the reverse method –
Python List reverse() Method
As you can see in the above program, we have a simple list, and then we are trying to use the reverse method on the list. After that, we are printing our list, and we can see in the output, that the list is reversed. Let’s have a look at the output now -0
[31, 15, 29, 45, 12, 23]
As you can see, we have got our list reversed. So, whenever we need to reverse our list, we can make use of the reverse method. Alternatively, we can also do list slicing, to reverse a list. Let’s have a look at that as well. We simply need to omit the start and stop values, and then we have to provide the step as -1. Doing this will simply reverse the list. Let’s have a look at the program.
As you can see, we are using the slicing operator, to reverse the list. Reversing a list is very simple, and useful at times. We would use the reverse method as and when required in our Python programs. Remember that the reverse method reverses the list.