Home >Backend Development >Python Tutorial >Why Does `list.sort()` Return `None`?
Why Does "return list.sort()" Return None Instead of the List?
In the provided code snippet:
answer = newList.sort()
The newList.sort() method is used to sort the newList in place, meaning it modifies the original list without creating a new one. Hence, answer receives None as it doesn't refer to the sorted list but rather to the value returned by the sort() method.
To resolve this issue and return the sorted list, modify the code as follows:
newList.sort() return newList
This will ensure that the sorted newList is returned as the method result.
The above is the detailed content of Why Does `list.sort()` Return `None`?. For more information, please follow other related articles on the PHP Chinese website!