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

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
229,732 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:]