I was working out the solution to a problem when I came across the following...
I had started by creating a variable, test
, and initializing it to a list.
test = [3, 2, 1]
After playing around with .sort()
and calling it upon my list, test
, I tried to use print as follows:
print test.sort(reverse = True)
The output was None
and I was just curious as to why exactly this is.
sort()
modifies the list in-place, and returns None
. This is unlike the sorted()
function, which returns a reference to the sorted list.
It could return a reference as well (like JavaScript's sort()
), but it doesn't. It's a design decision .
Sort works in place. It does not return new list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.