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

Python how to cast tuple to list and apply dot notation

Why does:

x = (5,4,3)
y = list(x)
y.sort()

work, but

x = (5,4,3)
y = list(x).sort()

not work?

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

>Solution :

y = list(x)

this calls the function list on x and puts the result in y

y = list(x).sort()

this calls the function list on x, call .sort() on the resulting list, then store the return of .sort() in y, the return of .sort() is None.

the list is sorted in memory but the sorted list is thrown away, and only None is stored in y.


relevent What is the difference between sort and sorted

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