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

sorted_things

Hi, there! I'm trying to use the .sort() command for sorting out the list on favorite_things. I'm not sure if I'm just missing something very simple for the list to be recognized. Any tips!

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:8]
favorite_things = sorted_things
sorted_things.sort()

2 Answers

Matthew Long
Matthew Long
28,407 Points

You've almost got it except sorted_things = favorite_things doesn't actually create a new array. There is still only one array, where sorted_things is a reference to favorite_things. To get around this you can slice it. You can use the slice to create a copy by slicing the original array from beginning to end, [:]. This was the very last topic discussed on slices in the last video.

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

sorted_things = favorite_things[:]
sorted_things.sort()
Mathieu St-Vincent
Mathieu St-Vincent
9,244 Points
slice1 = favorite_things[1:4]

slice2 = favorite_things[-2:]       # Takes the last 2 items of the array
sorted_things = favorite_things[:]  # You reversed the variable order. Also, use [:] to copy without reference

sorted_things.sort()