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

Suleiman Abdurozikov
Suleiman Abdurozikov
2,943 Points

Can anyone help

Can t figure out how should I assign an iterable and then and return 4 items

slices.py
def first_4(Hello there):
Lisa Pitts
Lisa Pitts
3,249 Points

You're going to want to use a slice. I used the slice [:4] to return the first four items of the iterable.

I'm not sure if this matters or not, but I didn't use any spaces in my argument when I did it. For example, something like this:

def first_4(iterable)

1 Answer

First, you can't have spaces in variable/parameter names.

Second, you should be returning the first 4 items in the array with the slice method, like this:

some_list[:4]

The colon in front of the 4 means that we want the 4th item (not the 5th item!) and all the elements before.

You can do similarly with the colon in front:

some_list[4:]

This will get the items starting from 4 and all the elements after.

So, to complete this task, you should do:

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

Good luck! ~alex