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

Hi, I'm stuck in python collections stage 2 slices.

I pass task one and two.. but on task three it fails and asks me to now fixt task 1 again.. ? my script 'works' in Python 3.4.1

def first_4(the_list):
    firstFour = the_list[:4]
    print(firstFour)


def odds(the_Next_List):
    odd_numbers = the_Next_List[1::2]
    print(odd_numbers)


def first_and_last_4(the_Last_List):
    first_four = the_Last_List[:4]
    last_four_rev = the_Last_List[len(the_Last_List):5:-1]
    last_four = last_four_rev[::-1]
    firstLast = first_four + last_four
    print(firstLast)

the_list = list(range(1,11))
the_Next_List = list(range(1,11))
the_Last_List = list(range(1,11))

first_4(the_list)
odds(the_Next_List)
first_and_last_4(the_Last_List)

All help and suggestions will be greatly appreciated. Thank you.

2 Answers

Hi Lee,

For posting code, see this thread: https://teamtreehouse.com/forum/posting-code-to-the-forum

First of all, you should be using return instead of print(). The challenge is asking you to return the values, not to print them.

To get the last 4 items you can specify a negative starting value and that will count from the end.

the_Last_List[-4:] means to start at the 4th element from the end, and then go all the way to the end. Giving you the last 4 items.

def first_4(the_list):
    return the_list[:4]

def odds(the_Next_List):
    return the_Next_List[1::2]


def first_and_last_4(the_Last_List):
    return the_Last_List[:4] + the_Last_List[-4:]
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You could also reverse the list, take the first 4 items, and then reverse them to, in effect, have the last 4 items. More cumbersome than the_Last_List[-4:] but not a terrible solution.

Hi Kenneth,

Thanks for the alternative solution. Is this more in line with what is taught up to this point? I haven't been through this course yet so I apologize if I've jumped ahead and introduced something they weren't supposed to know yet.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Jason Anello No, working through the course, I'd expect the the_list[-4:] solution. But since the OP had already done a list reversal thing (but only of a slice), I thought I'd point out that they could have reversed the list, sliced the first four, then reverse that slice to get what they want, too.

Hi Jason Anello, thank you for the tip on posting code, and your coding help.. the_list[-4:] is a much better solution. thanks again. Kenneth Love thanks for your help too :)