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

Emilio Andere
Emilio Andere
495 Points

WHY IS THIS WRONG?

I dont know what to put in the second slice

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

first_4([2, 7, 9, 4, 6])

def first_and_last_4(list2)
    return my_list[1:5] 
    return my_list[:5]

first_and_last4([3, 7, 9, 5, 4, 9, 1, 6, 6, 4, 0])

1 Answer

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Emilio,

First and foremost, you cannot return twice from a function. As soon as python runs return it stops executing the function. Nothing else will be run. So you'll need to combine the slices first, and then return them.

Second - don't forget the colon after the first line of your function!

Last you need to figure out how to get the last four items of the iterable. Right now your two return statements return the second through fifth, and then the first through fifth. Neither of those are right. You know how to get the first four (you did it in part 1), now you need to get the last four. You might need to watch the video again to remind yourself how to get items from the end of an iterable.

Good luck, and let us know how it goes!

Cheers :beers:

-Greg