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

Great work! OK, let's do another test. Get the last two items from favorite_things and put them into slice2.

I don't understand how to to get the last two items is the list and slice them.

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

6 Answers

Morten Ingemann
Morten Ingemann
5,428 Points

You can do it like this: When using slices you can leave out the index either before or after the colon. This will make the slice look through the entire list. You can go through the list from the back using "-", that way it will take the last two items on the list.

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:]
Michael Moore
Michael Moore
7,121 Points

I couldn't understand why mine wasn't working. Stupid typo. Typed favorite_thing instead of favorite_things. Its always a typo. Anyways, here is my answer.

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:]
Abdishakur Hassan
Abdishakur Hassan
3,585 Points

You can use - negative sign to Access last two items. Forexample lΓ€st 3 items is [-3:]

Sean Hernandez
PLUS
Sean Hernandez
Courses Plus Student 9,350 Points

Slices don't slice the last index item because the end index of slice tells python where to stop but does not include the value at the index. To work around this we can tell where to start the slice with no end index and python will know to stop when it reaches the end of the length of favorite_things. For example favorite_things[0:2] will pull values from index 0 and 1 but will stop at index 2 but not return the value at index 2. Using that if we tell python favorite_things[0:] with no ending index, it will add all the values from the start to finish. To solve the ending issue try favorite_things[-2:] or favorite_things[5:]. Hope this helps!!!

macmondiaz
macmondiaz
1,415 Points

Hello! but how come this doesn't work?:

slice2 = favorite_things[6:]
slice2 = favorite_things[6:999]
Michael Moore
Michael Moore
7,121 Points

Because you are numbering the 6th item and not the index position. it should be

slice2 = favorite_things[5:]
# or
slice2 = favorite_things[5:999]

slice2 = favorite_things[5:]