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

Brandon Harrison
Brandon Harrison
1,283 Points

my function to return the first 4 item in an iterable will not work.

What am I missing? should I just give up on this coding thing at this point since I have to get help on these stupid things that seem so freaking simple?

slices.py
def first_4(iterable):
    return iterable[0,3]

I believe when you do slices you put the second argument one number past what you want the last number to be, so try [0, 4]. It should add everything up to but not including the 4 index.

def first_4(iterable):
    return iterable[0:4]

1 Answer

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

The return should look like this:

return iterable[0:4]

You forgot colon & need the number 4

Yep it's all about remembering all of the syntax- I even forgot that it requires a colon vs. a comma. I've been working on java more so than Python lately ;)