Why does “return list.sort()” return None, not the list?

前端 未结 7 705
感情败类
感情败类 2020-11-21 06:55

I\'ve been able to verify that the findUniqueWords does result in a sorted list. However, it does not return the list. Why?

def fin         


        
7条回答
  •  死守一世寂寞
    2020-11-21 07:33

    Python habitually returns None from functions and methods that mutate the data, such as list.sort, list.append, and random.shuffle, with the idea being that it hints to the fact that it was mutating.

    If you want to take an iterable and return a new, sorted list of its items, use the sorted builtin function.

提交回复
热议问题