Why does print(list.sort()) result in None?

36 Views Asked by At

In trying to understand how to work with classes, objects, and methods, Please explain why print(list.sort()) does not work like list.sort() followed by print(list).

list = [5, 1, 2]

print(list.sort())

#output is None

VS.

list.sort()

print(list)

#output is [1, 2, 5]

Explained above: I expected print(list.sort()) to output a sorted list instead of None

2

There are 2 best solutions below

0
Artemis On BEST ANSWER

As you can read in the documentation of list.sort

This method modifies the sequence in place for economy of space when sorting a large sequence. To remind users that it operates by side effect, it does not return the sorted sequence (use sorted() to explicitly request a new sorted list instance).

(bold text by me)

0
Aaxper On

list.sort() does not return any value. print(list.sort()) prints None because that is the value that represents no value.