Why does ‘.sort()’ cause the list to be ‘None’ in Python? [duplicate]

Simply remove the assignment from

result = result.sort()

leaving just

result.sort()

The sort method works in-place (it modifies the existing list), so it returns None. When you assign its result to the name of the list, you’re assigning None. So no assignment is necessary.


But in any case, what you’re trying to accomplish can easily (and more efficiently) be written as a one-liner:

max(len(Ancestors(T,x)) for x in OrdLeaves(T))

max operates in linear time, O(n), while sorting is O(nlogn). You also don’t need nested list comprehensions, a single generator expression will do.

Leave a Comment