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

A quiz question!

Great work! OK, this second one should be pretty similar to the first. 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.

slices.py
def first_4(thing):
    return thing[0:4]
def first_and_last_4(a):
    return a[0:4] 
    return a[-1:-5:-1]

1 Answer

Steven Parker
Steven Parker
231,236 Points

You can only use return one time.

When the function encounteres a return statement, it ends; so no further code will be performed in the function.

But you can combine values using concatenation ("+") in a single return statement.

Didn't know that! Thanks!

There is an error that says "Didn't get the right values." What should I write for the slices? Please help!

Steven Parker
Steven Parker
231,236 Points

You already know how to do the first 4. Just figure out how to do the last 4 and then concatenate those two.

I tried but I didn't get the answer. Could you help with the code? Thnx;)

Steven Parker
Steven Parker
231,236 Points

For the "last n" items you can use "-n" as the slice start value, and you won't need a stop or step value. For example, the last 7 items of a list name "month": "month[-7:]".

If you still have trouble, be sure to show your new code.