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 trialJuan Suarez
1,406 Pointsl.sort()
Why the return of the variable is None
l = [1,2,4,3] cleanL= l.sort() print cleanL
None
Juan Suarez
1,406 PointsSorry 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
37,150 PointsIt'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.
Juan Suarez
1,406 Pointsgreat many thanks!
1 Answer
Steven Parker
231,269 PointsThe 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.
Cheo R
37,150 PointsCheo R
37,150 Pointssort(), sorts in place, returning None. If you print l before and after calling sort you should see a sorted list.