Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

last 4 from a list

I must be missing something very minor. Any clue?

slices.py
# create a function named first_4

def first_4(given_list):

    list(given_list)  # make it a list
    return given_list[:4]  # return the first four items from whatever iterable is given

# create a function called first_and_last_4

def first_and_last_4(some_list)  # Accepts single iterable

    list_1 = some_list[:4]
    list_2 = some_list[-4:]
    new_list = list_1 + list_2
    return new_list # returns the first 4 and last 4 items as a single value

I cannot get the new_list to return the first 4 and last 4.

1 Answer

Blayne Holland
Blayne Holland
18,817 Points

Your code is absolutely correct. But Looks like theres a syntax error. Add a colon after the function declaration

def first_and_last_4(some_list):

colons and semi colons get me EVERY. SINGLE. TIME. very soon you'll be so good, you'll be able to spot them from miles away. Like a sniper from seal team 6 or something. happy hunting.

Oh my god ! - Cant believe it was that. Thank you much Blayne !