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

i need help with Python> Slices> intro to slices

intro to slices

here are my answers
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[0, 1, 2] slice1[:3]

5 Answers

steven can you put it in more blunt terms because it doesn't work

slice1 = favorite_things[0:3] does this work

Steven Parker
Steven Parker
243,318 Points

slice1 = favorite_things[0:3] :point_left: does the same thing as :point_right: slice1 = favorite_things[:3]
Either way gets you the first, second and third items.

But the challenge is to get the "second, third, and fourth items".

wait

but isn't it the answer?

slice1 = favorite_things[1:3]

then how come this does'nt work

Steven Parker
Steven Parker
243,318 Points

As I said before:

  • slice1 = favorite_things[1:3] :point_left: this gives you the second and third items
  • But the challenge wants the "second, third, and fourth items".

:point_right: Remember the "stop" value of a slice is the index number that the slice will go up to but not including.

Steven Parker
Steven Parker
243,318 Points

You have some extraneous stuff there.

But if we eliminate that first set of braces with the tuple (comma-separated numbers), and also remove the second reference to the variable being created, this would be left :point_right: slice1 = favorite_things[:3]

Now at that point you have a valid slice, but the instructions say the slice should include the "second, third, and fourth items", while this slice selects the first, second and third items. So you still need different start and stop values.

i tried slice1 = favorite_things[:3] i tried slice1 = favorite_things[1:3] i tried slice1 = favorite_things[1, 2, 3]

Steven Parker
Steven Parker
243,318 Points

Ok, let's examine each of those:

  • slice1 = favorite_things[:3] :point_left: as I said, this slice gives you the first, second and third items
  • slice1 = favorite_things[1:3] :point_left: this slice gives you the second and third items
  • slice1 = favorite_things[1, 2, 3] :point_left: this is not slice syntax

You were pretty close on that second retry. I bet you can revise that one and make it work.

Remember, the challenge wants the "second, third, and fourth items".