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 (Retired) Slices Slice Functions

Is there some crazy slice to get the first_4 and last_4 all in one slice?

I ended up having to do something like this:

def first_4_and_last_4(itr):
    first_4 = itr[:4]
    last_4 = itr[-4:]
    return first_4 + last_4

There isn't some crazy slice to get both the first 4 and last 4 in one step is there?

1 Answer

Hi undefish

I dont know any particular method that would just give your the first 4 and last 4 of an iterable.

just refactored your code a bit

def first_4_and_last_4(itr):
    return itr[:4]+itr[-4:]