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

Justin Carlson
Justin Carlson
12,755 Points

Python collections challenge not accepting my answer. any thoughts?

what are they looking for here:

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

I have been on and off with this and am sorry if its a simple question but I just cant figure this out today....

I have this in the answer:

def first_4(num):
  print(list(num[:4]))

and is says it returns none but if I test in in python i get this:

def first_4(num):
  print(list(num[:4]))

first_4([1,2,3,4,5,6])

outputs: [1, 2, 3, 4]

which makes this error REALLY annoying in the red box on top:

"Bummer! first_4() didn't return the right output. It returned None instead of [1, 2, 3, 4]."

looks like it returns that to me? what am I doing wrong here?

9 Answers

Simon Klit
Simon Klit
1,686 Points

Hi Justin,

It's really as simple as using Python's slice notation, which, simplified, is this:

a[start:end]

Try this one for this Code Challenge (also, make sure to return it, as asked):

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

first_4([66, 333, 222, 1, 1234])
Justin Carlson
Justin Carlson
12,755 Points

.... lol I didnt put a return statement in there.... THANK YOU!

been a long day and I am trying to do this and some other work at the same time.... seems to not be working out well :-(

Simon Klit
Simon Klit
1,686 Points

Great. Welcome!

Siddhant Shrivastav
Siddhant Shrivastav
17,320 Points

this worked for me:

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

def odds(arg2):
  return arg2[1::2]

def first_and_last_4(arg3):
  return arg3[:4] + arg3[-4:]
Simon Klit
Simon Klit
1,686 Points

Beat my solution, by leaving out that zero. Way to go!

Max Gallant
Max Gallant
15,500 Points

Can someone explain why this:

def first_and_last_4(iter)
  return iter[:4].extend(iter[-4:])

doesn't work, but using the plus symbol does work?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Max Gallant It doesn't work because iter[:4] is a Slice object, not a list so it doesn't have .extend().

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Just a hint, you'll never need a print() in a code challenge.

THis is working for me when I test it in workspaces with a "print" instead of a return to make sure I am getting the first four. Is this not also valid and correct code below?

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

first_4(20)
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Spencer Johnson You need to return the first four items from the iterable. You're creating a list from a range from what you're expecting to be an integer but is really an iterable (list, string, dict, tuple, etc).

Long Vo
Long Vo
1,142 Points

this hint is really helpful for beginners thanks!

Andrew Winkler
Andrew Winkler
37,739 Points

Why are you guys making 2 part methods?!?!!!???

def first_and_last_4(iterable):
  return first_4(iterable) + iterable[-4:]
  #fin

You've already written the first component of this equation in task one, so reference it, concatenate, and be done!

task 1 def odds(iterable): return iterable[1::2]

task2 def first_and_last_4(iterable): return iterable(range(1 [:4]

Task 3 def first_and_last_4(iterable): return iterable[:4] + iterable[-4:]

Simple code that works: x = [2,4,8,0,8,7,6,5] def first_4(x): return x[:4]

first_4(x)

ef first_4(carlton): return carlton[:4]

def first_and_last_4(carlton): return carlton[:4] + carlton[-4:]

def odds(carlton): return carlton[1::2]

Why won't this work? If I try this in the shell I return the first 4 items.

def first_4(): arg = list(range(20)) return arg[0:4]

90% of the treehouse challenges are way off the subjects. horrible!!