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

Darren Taylor
Darren Taylor
1,052 Points

Slices.py challenge.. I'm stuck, please help..!

Challenge Task 1 of 3.

The question reads:

Let's get in some slice practice! Create a new variable named slice1 that has the second, third, and fourth items from favorite_things.

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

3 Answers

Wesley Trayer
Wesley Trayer
13,812 Points

Here you go, Darren.

>>> 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[:]
>>> slice1[1:4] #This does not assign slice1 to the slice, it just prints these items from slice1
['whiskers on kittens', 'bright copper kettles', 'warm woolen mittens']
>>> print (slice1)
['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']

So slice1 is still equal to the whole list. To fix that.

>>> slice1 = favorite_things[1:4]
>>> print (slice1)
['whiskers on kittens', 'bright copper kettles', 'warm woolen mittens']

And now slice1 is equal to the slice of favorite_things.

Does this help you?

Darren Taylor
Darren Taylor
1,052 Points

Yes, Wesley, this definitely helps..! Thanks for your informative explanation and actual examples, I understand what’s going on now. Now I can pass this challenge with my eyes closed..!

Wesley Trayer
Wesley Trayer
13,812 Points

Remember, Python starts counting at zero, not one. So your slice,

slice1[2:5]

starts at the third item, not the second.

Yes, it is an odd way of counting.

Darren Taylor
Darren Taylor
1,052 Points

This doesn't work, but thanks for trying! "Any other suggestions..?"

Daniel Cunningham
Daniel Cunningham
21,109 Points

The challenge asks you to create a variable named slice1 that contains only the second, third, and fourth items from favorite_things. You have named a variable slice1 that contains ALL of favorite_things and then separately pulled the 3rd, 4th and 5th items. The test will not check for that additional code, only what is contained in slice1.

Make sure your slice one contains ONLY the second, third and fourth elements. As Wesley mentioned, arrays start counting at 0, so your slice needs to be adjusted accordingly.

Darren Taylor
Darren Taylor
1,052 Points

Hi Daniel, my code above works fine in Python but for some reason it doesn’t work in this challenge. Visual eamples would be helpful! Thanks