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

I can't do 2 slices in a single line

Hey guys, in the first_and_last_4, how can I slice the first 4 and the last 4 items at the same time? I did it in 2 steps why doesn't it work?

Thank you :)

slices.py
def first_4(iterable):
    return iterable[:4]

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

1 Answer

Hi there!

You're slicing is spot on, but you have two return statements. Don't forget that a return statement exists your function, so the second return line is never executed.

As for slicing the first four and last 4 at a time, sadly you can't - slice only returns one list covering the pattern it was given but you could use multiple calls in one line:

head,tail = iterable[:4],iterable[-4:]
return #???

Do you know a way to concat head and tail? hint: they could both be strings... anyway, that's what you'd put in the return line...

Hi Jon thanks for your precious help!

I concatenate it like this :

def first_and_last_4(ite):

head = ite[:4]
tail = ite[-4:]

concat = head + tail
return concat

;) a big hug!