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 trialMadhan Mohan Reddy Paidimarla
11,676 Pointscreate a function named first_4 that returns the first four items from whatever iterable is given to it.
create a function named first_4 that returns the first four items from whatever iterable is given to it.
first_4 = 1,2,3,4
1 Answer
James Arnold
3,986 PointsFirst thing, it's asking us to create a function instead of a just a variable like what we have here. To create a function we'll use:
def first_4(ctx):
return
So now that we have our function defined and we're passing in our value - ctx. We need to define the function to slice off the first four elements. This can be done using list slicing like so:
def first_4(ctx):
return ctx[:4]
Tlotlo Molokwe
16,618 PointsTlotlo Molokwe
16,618 Pointstry def first_4(num): return num[0:4]