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

sidni ahmed
sidni ahmed
3,329 Points

Make a function named first_4 that accepts an iterable as an argument and returns the first 4 items in the iterable.

what do i do here?

slices.py

2 Answers

Clayton Perszyk
MOD
Clayton Perszyk
Treehouse Moderator 48,850 Points

Hey Sidni,

The question is asking that you take an iterable (i.e. a python list ) and extract the first four items; you will want to use a slice method witch looks something like this: list[start:end], where the list is the iterable and the start is where you want to begin extracting items and the end is where you want to end extracting. Here's an example using a string: "hello world"[6:11] would return 'world'. Here's a link to more information in the python documentation: https://docs.python.org/2/tutorial/introduction.html#lists

best, Clayton

sidni ahmed
sidni ahmed
3,329 Points

thanks alot. I really love the treehouse community. Everyone is so helpful. this is the code i did:

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

now i am hoping i can get past the other two steps

Clayton Perszyk
Clayton Perszyk
Treehouse Moderator 48,850 Points

Glad I could help. Good look with the rest of the challenges!