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

Enzie Riddle
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Enzie Riddle
Front End Web Development Techdegree Graduate 19,278 Points

Slices

Here's the question: Alright, these tasks are a bit harder than the others so far in this course but I know you can do them! In this first one, create a function named first_4 that returns the first four items from whatever iterable is given to it.

I am quite lost. What am I doing wrong? Please explain in a way that is easy to understand: I am new at this!

slices.py
iter = input("List at least four items")
first_4 = return iter[0:4]

2 Answers

Your slice technique is spot on, it's just the function part.

create a function named first_4 that returns the first four items from whatever iterable is given to it.

remember that we define a function like thus:

def a_function(a, b):
    return a + b
Katherine Pawlak
Katherine Pawlak
12,609 Points

Use the function formatting as the previous post suggests:

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