I have example list like this:
l = [1,2,3]
print(l.reverse())
#give None instead [3, 2, 1]
How is that? How can I reverse list?
Answer
How can I reverse list?
The list is being reversed. But list.reverse
reverses in place, and does not return anything to the caller. You can verify this by printing the list after calling reverse
on it:
>>> l = [1,2,3]
>>> l.reverse()
>>> print(l)
>>> [3, 2, 1]
If in doubt, use the help:
>>> help(list.reverse)
Help on method_descriptor:
reverse(...)
L.reverse() -- reverse *IN PLACE*
(Python 2.7 documentation)
No comments:
Post a Comment