Why does "return list.sort ()" return None, not a list?

I was able to verify that findUniqueWords leads to sorting the list . However, this is not a return list , why?

 def findUniqueWords(theList): newList = [] words = [] # Read a line at a time for item in theList: # Remove any punctuation from the line cleaned = cleanUp(item) # Split the line into separate words words = cleaned.split() # Evaluate each word for word in words: # Count each unique word if word not in newList: newList.append(word) answer = newList.sort() return answer 
+122
python sorting list return
Sep 04 '11 at 17:58
source share
7 answers

list.sort sorts the list in place, i.e. it does not return a new list. Just write

 newList.sort() return newList 
+147
Sep 04 '11 at 18:00
source share
— -

The problem is here:

 answer = newList.sort() 

sort does not return a sorted list; rather, it sorts the list in place.

Using:

 answer = sorted(newList) 
+118
04 Sep '11 at 18:00
source share

Here is a letter from Guido van Rossum in python dev listifyig why he prefers not to return self for operations that affect the object and not to return a new one.

This comes from the coding style (popular in different languages, I believe, especially Lisp revels in it), where a number of side effects on a single object can chain the chain as follows:

  x.compress().chop(y).sort(z) 

which will be the same as

  x.compress() x.chop(y) x.sort(z) 

I believe that the chain poses a threat to readability; this requires the reader to be familiar with each of the methods. the second form makes it clear that each of these calls acts on the same object, and therefore even if you do not know the class and its methods very well, you can understand that the second and third calls apply to x (and that all calls are made for their side effects), not something else.

I want to reserve a chain for operations returning new values, like string processing operations:

  y = x.rstrip("\n").split(":").lower() 
+56
02 Oct. '12 at 23:16
source share

Python has two kinds of sorting: a sorting method (or "member function") and a sorting function. The sort method works on the contents of an object with a name - consider it as the action that the object takes to reorder. A sort function is an operation on the data represented by an object, and returns a new object with the same contents in sorted order.

Given a list of integers named l , the list itself will be reordered if we call l.sort() :

 >>> l = [1, 5, 2341, 467, 213, 123] >>> l.sort() >>> l [1, 5, 123, 213, 467, 2341] 

This method has no return value. But what if we try to assign the result of l.sort() ?

 >>> l = [1, 5, 2341, 467, 213, 123] >>> r = l.sort() >>> print(r) None 

r now actually means nothing. This is one of those strange, somewhat annoying details that a programmer is likely to forget after a period of absence in Python (that’s why I write this, so I don’t forget again).

The sorted() function, on the other hand, will do nothing with the contents of l , but will return a new sorted list with the same contents as l :

 >>> l = [1, 5, 2341, 467, 213, 123] >>> r = sorted(l) >>> l [1, 5, 2341, 467, 213, 123] >>> r [1, 5, 123, 213, 467, 2341] 

Remember that the return value is not a deep copy , so be careful with side effects on the items contained in the list, as usual

 >>> spam = [8, 2, 4, 7] >>> eggs = [3, 1, 4, 5] >>> l = [spam, eggs] >>> r = sorted(l) >>> l [[8, 2, 4, 7], [3, 1, 4, 5]] >>> r [[3, 1, 4, 5], [8, 2, 4, 7]] >>> spam.sort() >>> eggs.sort() >>> l [[2, 4, 7, 8], [1, 3, 4, 5]] >>> r [[1, 3, 4, 5], [2, 4, 7, 8]] 
+13
Nov 10 '15 at
source share

Typically, Python returns None from functions and methods that mutate data, such as list.sort , list.append and random.shuffle , even though it hints that it mutated.

If you want to iterate and return a new, sorted list of your items, use the built-in sorted function.

+12
Sep 04 '11 at 19:01
source share

To understand why it does not return a list:

sort () does not return any value.

The sort () method simply sorts the items in a given list in a specific order - ascending or descending, without returning any value.

So the problem is answer = newList.sort() where there is no answer.

instead, you can simply return newList.sort()

The syntax for the sort () method is:

 list.sort(key=..., reverse=...) 

Alternatively, you can also use the Python built-in sorted () function for the same purpose.

 sorted(list, key=..., reverse=...) 

Note. The simplest difference between sort () and sorted () is: sort () does not return any value, and sorted () returns an iterable list.

So in your case answer = sorted(newList)

+3
Dec 06 '18 at 12:58
source share

Please re-read the documentation for the Python sort function, focusing on its return value.

-6
Sep 04 '11 at 18:00
source share



All Articles