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
atoniolo
9,216 PointsCannot Get this question in python collections, back and forth
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']
def slice1(favorite_things): favorite_things.slice(1,2,3) return favorite_things
I do not know what I did wrong.
1 Answer
ursaminor
11,271 Pointsslice() is not a list method so you can't call it on the favorite_things list like you're doing. It creates a slice object that you can use to get items from a list. You can get the items created by the slice object by using the list index method:
favorite_things[slice(1, 2, 3)]
However, this won't actually change favorite_things, so when you return favorite_things it will be the same as before. You need to store it in another variable and return that.
result = favorite_things[slice(1, 2, 3)]
return result
Also, not sure what you're trying to get, but the syntax is slice(start, stop, step) so you'll only end up with the second item in the list with slice(1, 2, 3)
I think an easier way is using slice index notation:
some_list[start: stop: step]
https://stackoverflow.com/questions/509211/understanding-pythons-slice-notation