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

Looking for help with Slices and steps 🙏

Thanks for taking a look. Here is the challenge / objective I am working on:

"Make a new function named first_and_last_4. It'll accept a single iterable but, this time, it'll return the first four and last four items as a single value."

My understanding of this objective is to use two slices of the list; one for the first four value and one for the last four values. Then combine them to satisfy the single value criteria.

The code I wrote which I believe satisfies this is below. The slices seem to pull the first and last four values of any list I test it on but I think it is the single value item I getting tripped up on. I'm not necessarily looking for a solution but more to be pointed in the correct direction. Thanks in advanced!

slices.py
def first_and_last_4(iterable):
    return iterable[:4] + iterable[-1:-5:-1]

1 Answer

Hi Austin,

The devil is in the details. Your code does return the first and last 4 values in any list. But, to point you in the "right direction," look carefully at how your last 4 values compare to the original list.

Hope this helps.

Yes! Thank you, Mark! Your response is exactly what I needed. I had a feeling I was overthinking this one. Cheers