I used two different ways to create a reverse list.
The first way:
>>> a=list(range(3))
>>> a.reverse()
>>> print(a)
[2,1,0]
The second way:
>>> a=list(range(3)).reverse()
>>> print(a)
None
Why does the second way does not work?Thanks for any help.
It fails because
reverse
changes the list in place (i.e. it does not create a new list) and like most functions that operate in place it returnsNone
.In your first example
It works fine as you capture
a
as the return value fromlist(range(3))
, which returns alist
. You then reverse that list in place and print it. All fine!In your second example
a
is equal to the return value ofreverse()
(notlist(range(3))
).reverse
is called on a temporary list and returnsNone
, which is the value assigned toa
.