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

Ronald Tse
Ronald Tse
5,798 Points

What's the problem with my code

I have followed the instruction:

  1. create a function called "first_4"
  2. return the first 4 item
slices.py
def first_4(iterable):
    return iterable[0:3]

1 Answer

Chase Marchione
Chase Marchione
155,055 Points

Hi Chi,

The stop value is not included when you do a slice in Python (that, and the first value would be at 0... thus, you're returning items 0, 1, 2 and 3, which are the first four items of the iterable), so the challenge is actually looking for:

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

Hope this helps!