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

William Forbes
William Forbes
21,469 Points

Help with first_and_last_4?

Can you see my code and see if you can figure out the heck Im doing wrong?

slices.py
def first_and_last_4(iterable):
    iterable_stage = iterable[:4]
    iterable_stage = iterable_stage + iterable[-4:]
    return iterable_stage
William Forbes
William Forbes
21,469 Points

Thank you!! And thats how I had the code originally I just changed when i couldnt get it working lol.

2 Answers

Steven Parker
Steven Parker
221,451 Points

Remember each step should add to the previous step(s).

So your original first_4 function should remain as it was for task 1, and the new function should be added to the same file (or "tab" in this case).

Otherwise, that should do just fine for task 2. :+1:

Kurt L
Kurt L
22,856 Points

It looks like it should work. Just be sure not to delete the first function from part 1:

def first_4(iterable):
    return iterable[:4]

You might also want to simplify your second function as:

def first_and_last_4(iterable):
    return iterable[:4] + iterable[-4:]