Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Slices Introduction To Slices

l.sort()

Why the return of the variable is None

l = [1,2,4,3] cleanL= l.sort() print cleanL

None

Cheo R
Cheo R
37,150 Points

sort(), sorts in place, returning None. If you print l before and after calling sort you should see a sorted list.

Sorry not sure if you are explaining this when you are saying sort in place, but why the sorted list is not assigned to the variable cleanL

Cheo R
Cheo R
37,150 Points

It's like l is sorted, then reassigned to l, the sort function then returns None, which in your example is assigned to cleanL. So when you print cleanL, you see None. Try printing your l variable before and after calling the sort function on it, also trying calling l.sort() inside a print function to see what it returns.

great many thanks!

1 Answer

Steven Parker
Steven Parker
231,275 Points

The sort method does not return anything.

As Cheo pointed out, it just modifies the array it is applied to. Try this instead:

cleanL = l = [1,2,4,3]
cleanL.sort() 
print(cleanL)

Code formatting is particularly important for Python (in Python, indentation is everything). So when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down: