Why doesn't this work in Python?
>>> print [0,1,0,1,1,0,1,1,1,0,1,1,1,1,0].reverse()
None
I expected to get back the list in reverse order.
You can reverse a string in Python using slicing or the reversed() method. A recursive function that reads each character in a string and reverses the entire string is another common way to reverse a string. There is no function explicitly designed to reverse a string.
Python List reverse() is an inbuilt method in the Python programming language that reverses objects of the List in place i.e. it doesn't use any extra space but it just modifies the original list.
Every list in Python has a built-in reverse() method you can call to reverse the contents of the list object in-place. Reversing the list in-place means won't create a new list and copy the existing elements to it in reverse order. Instead, it directly modifies the original list object.
In Python, there is a built-in function called reverse() that is used to reverse the list. This is a simple and quick way to reverse a list that requires little memory. Syntax- list_name. reverse() Here, list_name means you have to write the name of the list which has to be reversed.
>>> a = [3, 4, 5]
>>> print a.reverse()
None
>>> a
[5, 4, 3]
>>>
It's because reverse()
does not return the list, rather it reverses the list in place. So the return value of a.reverse()
is None
which is shown in the print
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With