Python Sort() method

I am starting to learn Python.

Can someone explain why sort() returns None?

alist.sort()            ## correct
alist = blist.sort()    ## NO incorrect, sort() returns None

Why shouldn't

alist = blist.sort()

return the sorted list and give it back to alist? This does not make sense to me.

Thanks.


alist.sort() sorts alist in-place, modifying alist itself.

If you want a new list to assign somewhere, use blist = sorted(alist)

  • list.sort(): http://docs.python.org/library/stdtypes.html#mutable-sequence-types
  • sorted(): http://docs.python.org/library/functions.html#sorted

Answering to his question, it returns none because the method always returns None. When you use it, it automatically modifies the list, so it does not keep the original intact (ie it does not return a sorted copy of the list).