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

Cant sort list

for some reason I cant get past challenge question 3. Sorting the list.

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']

slice1 = favorite_things[1:4]
slice2 = favorite_things[-2:]
sorted_things = favorite_things[:]
sorted_things = favorite_things.sort()

What am I missing?

slices.py
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']

slice1 = favorite_things[1:4]
slice2 = favorite_things[-2:]
sorted_things = favorite_things[:]
sorted_things = favorite_things.sort()

2 Answers

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Nick,

The instructions tell you:

Then use .sort() to sort sorted_things.

You are using .sort() to sort favourite_things.

Note that sort() sorts a list in-place and returns nothing, so when you do this:

sorted_things = favorite_things.sort()

You are changing favorite_things by applying sort(). It then returns nothing, so you are assigning None to sorted_things.

Cheers

Alex

like this:

favorite_things = sorted_things[:]
favorite_things = sorted_things.sort()
Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Nick,

Let's take a look at the instructions (task 3). You are being asked to:

Make a copy of favorite_things and name it sorted_things.

You are doing the opposite: making a copy of sorted_things and naming it favorite_things. Depending on what other code you have that is not included in your snippet, you may not even have a variable called sorted_things to make a copy of, so you'll probably get an error that sorted_things is not defined: NameError: name 'sorted_things' is not defined.

Next you are being asked to:

Then use .sort() to sort sorted_things.

In your line:

favorite_things = sorted_things.sort()

You are doing that (the code on the right side of the assignment operator), but then you are also assigning the value of None to favorite_things which you were not told to do (and will thus get the following error: Don't change or sort `favorite_things`!).

Cheers

Alex