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 trialScott Ewing
3,107 PointsWhy doesn't "sorted_things = favorite_things[:].sort()" create a sorted copy in sorted_things?
I tried to use a one-line assignment to create the sorted list of 'sorted_things' but it doesn't work. It seems that favorite_things[:].sort() returns a NoneType when I would expect it to return a list.
ex:
>>> favorite_things = ['raindrops on roses', 'whiskers on kittens', 'bright copper kettles',
'warm woolen mittens', 'bright paper packages tied up with string',
'cream colored ponies', 'crisp apple strudels']
The result is what is expected here:
>>> favorite_things[:]
['raindrops on roses', 'whiskers on kittens', 'bright copper kettles', 'warm woolen mittens', 'bright paper packages tied up with string', 'cream colored ponies', 'crisp apple strudels']
Now if we try and sort this in-line we get nothing:
>>> favorite_things[:].sort()
>>>
If we assign this to a variable, we still get nothing:
>>> sorted_things = favorite[:].sort()
>>>
>>> print(sorted_things)
None
>>>
1 Answer
Steven Parker
231,236 PointsWell, "sort()" might not work as you expect. It directly sorts the thing you apply it to, but doesn't actually return anything. And since you were applying it to an anonymous slice, there was no evidence left of what it did.
You probably want to use "sorted()" instead, it returns a sorted version of its (unchanged) argument:
sorted_things = sorted(favorite_things)