Mark Bryan Yu wrote:
This set of codes works:

x = range(5)
x.reverse()
x
[4, 3, 2, 1, 0]

But this doesn't:

x = range(5).reverse()
print x
None

Please explain this behavior. range(5) returns a list from 0 to 4 and
reverse just reverses the items on the list that is returned by
range(5). Why is x None (null)?
--
http://mail.python.org/mailman/listinfo/python-list
Because you are misusing reverse. It is an operation on a list that reverses the list in place. It is meant to be used only in the situation where you already have the list and want to reorder it:

x = .... list ...
x.reverse() # reorders, but returns nothing
... now use x ...


You might look at the builtin reversed(list): http://www.python.org/doc/faq/programming/#how-do-i-iterate-over-a-sequence-in-reverse-order

Gary Herron



--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to