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

This works for me, when I test it in Workspaces, but it is throwing an error????? Is this also a correct option?

in workspaces, I am adding 'print' in front of first_4(20) and it is printing back the 1st four [0, 1, 2, 3] in the range of 20. So why is this failing the coding challange...

def first_4(i): iterable = list(range(i)) return iterable[:4]

first_4(20)

slices.py
def first_4(i):
  iterable = list(range(i))
  return iterable[:4]

first_4(20)

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi, Spencer

in workspaces, I am adding 'print' in front of first_4(20)

No, the argument for this function needs to be an iterable, 20 is not; iterables in Python are list, string ....

the answer is actually pretty simple.

def first_4(i):
  return i[:4]   # slice out the first 4 items in the iterable

Thank you

Thank you, I changed it to code below and then tested in workspaces and it passed.

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

print(first_4(list(range(20))))