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

Need help with Python

When I try to run the code I get "Bummer: TypeError: first_4() takes 0 positional arguments but 1 was given" I am trying to return the first 4 iterables

slices.py
def first_4():
    return first_4[0:5]

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Vincent,

The instructions tell you:

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

This is Treehouse's way of telling you that they want you to write a function that takes an argument, which will be an iterable.

Inside your function, instead of calling the function again, you should be returning the elements from the iterable that are supplied as an argument when calling your function.

Also, your square brackets are selecting the wrong number of items. Remember that a slice starts with the index that corresponds to the first value and ends immediately before the index corresponding to the second value. So consider the following example:

my_list = ['hello', 'to', 'you']
print(my_list[1:2])

In the example we will print all the items from index 1 ('to') to just before index 2 ('you'), i.e., just 'to'.

Hope that helps

Alex