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 Slice Functions

Richard Li
Richard Li
9,751 Points

First Slice Challenge Task 2 of 4: Why my code below is not right?

def first_and_last_4(item):
        a = item[:-5:-1]
        a.sort()
        return item[:4] + a

I am using this way to get familiar with steps and negative index. I know that item[:4] + item[-4:] will work too. But in REPL my above code have the same output as this one. So I don't know why it is not checked OK by the checker?

My guess is that .sort() messes up with the checker since the data it passed in may not be numeric or sorted?

1 Answer

Steven Parker
Steven Parker
229,744 Points

It sounds like you already spotted part of the problem, you won't want to sort anything here.

But also take another look at your parameters for the "last four" slice. Unlike the "first four", you'll want to set the start position this time instead of the end position. And you won't want to reverse the order.

A little experimentation with slices in the workspace or your locally installed Python might be very helpful.