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

David P
PLUS
David P
Courses Plus Student 1,162 Points

What is wrong with my code???

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 = [1:3]
Kyle Horne
Kyle Horne
3,843 Points

I believe it needs to be slice1 = favorite_things[1:4]

The second element refers to index 1, third element to index 2, and fourth element to index 3. The slice will include index 1 all the way up to but not including index 4.

3 Answers

Steven Parker
Steven Parker
231,275 Points

I see two issues:

  • the slice numbers in brackets must come directly after the name of the list
  • as Kyle pointed out, the "stop" number is the index to stop before

slice1 = favorite_things[1:4]

Joel Williams
Joel Williams
800 Points

Sorry to necro this, but just now going through the program. Maybe this would be helpful to others in the future.

I did the same thing and over-complicated it. I wrote mine as:

slice1 = favorite_things[:] slice1[1:4]

When it really just needed to be "slice1 = favorite_things[1:4]" for the challenge. Although, mine wasn't wrong, it was just less concise with an extra step in there.