Python Python Collections (2016, retired 2019) Slices Slice Functions

Francisco Luttmann
Francisco Luttmann
2,406 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.

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,406 Points

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