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

Francisco Luttmann
Francisco Luttmann
2,696 Points

first_and_last_4 challenge problem help

I am wondering if anyone can tell me what I am doing wrong with this challenge problem. The problem asks to make a function that returns the first and last 4 items in a list "as a single value" I have run the attached code in a workspaces script with some print functions added in and it seems to be outputting what I want, but the work checker doesn't like it. I am wondering if it has something to do with that last "as a single value" part that I am just not understanding.

Thanks

slices.py
def first_4(iterable):
    result = iterable[:4]
    return result
def first_and_last_4(iterable):
    first_4 = iterable[:4]
    last_4 = iterable[-1:-5:-1]
    result2 = first_4 + last_4
    return result2

1 Answer

in last_4 u want the last 4 elements so u just need to set this value to [-4:] which means four from the end

Francisco Luttmann
Francisco Luttmann
2,696 Points

That makes sense. I don't know why I was trying to grab the last 4 items going backwards. Thanks!