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 Back and Forth

Why doesn't this work?

Hi Guys,

I tried this for the last step of this code challenge but was told that it wasnt sorted ? Can someone explain why this is wrong?

Thanks

Sam

sorted_things = favorite_things[:].sort()

1 Answer

Stuart Wright
Stuart Wright
41,118 Points

It is because the .sort() method sorts the list in place, and doesn't return anything, so assigning the result of the .sort() method to a new variable doesn't do what you think it would do. For this challenge, you need to use two lines:

sorted_things = favorite_things[:]
sorted_things.sort()

A one-line solution would be:

sorted_things = sorted(favorite_things[:])

But the challenge specifically asks for you to use the .sort() method here, so the first solution is the one to pass the challenge.