Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

list.sort()

print(list)

#output is [1, 2, 5]

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

>Solution :

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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading