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

harpreet grewal
harpreet grewal
19,018 Points

i dont understand what im doing wrong

it keeps saying im trying to sort favorite_things

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[5:7]

sorted_things = favorite_things sorted_things.sort()

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[5:7]

sorted_things = favorite_things
sorted_things.sort()

3 Answers

Hey Harpreet,

Check out my code, I edited your code and added comments:

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

# notice the [:]
sorted_things = favorite_things[:]
sorted_things.sort()

Notice the [:], we need this because without it you would be changing the variable name of favorite_things instead of assigning a copy "[:]" of favorite_things.

If you have any other questions I will update my answer, if you do not have any other questions:

Remember to upvote and to choose the best answer so that your question receives a checkmark in forums.

Kind regards,

Leo

You have to add a [:] after the sorted_things = favorite_things so it looks like this: sorted_things = favorite_things[:].


This is because in your original script, you assign the sorted_things variable to point to the same point in memory as favorite_things, not to get the value of favorite_things and assign it to sorted_things. So, this means if you edit either lists, both lists will be changed in place since they point to the same place in memory. You can use [:] to get the a copy if the list.


I hope you understand. ~Alex

Don't worry if you don't completely understand; this is a very confusing thing in computer science. Just remember to add [:] if you want to set variable to another variable containing a list.

harpreet grewal
harpreet grewal
19,018 Points

Thank You Leonard and Alex!