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 trialJustin Carlson
12,755 PointsPython 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
1,686 PointsHi 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])
Siddhant Shrivastav
17,320 Pointsthis 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
1,686 PointsBeat my solution, by leaving out that zero. Way to go!
Max Gallant
15,500 PointsCan 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
Treehouse Guest TeacherMax Gallant It doesn't work because iter[:4]
is a Slice
object, not a list so it doesn't have .extend()
.
Kenneth Love
Treehouse Guest TeacherJust a hint, you'll never need a print()
in a code challenge.
Spencer Johnson
19,708 PointsTHis 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
Treehouse Guest TeacherSpencer 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
1,142 Pointsthis hint is really helpful for beginners thanks!
Andrew Winkler
37,739 PointsWhy 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!
Annie Scott
27,613 Pointstask 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:]
Juris Kalnins
2,797 PointsSimple code that works: x = [2,4,8,0,8,7,6,5] def first_4(x): return x[:4]
first_4(x)
Carlton M Mandikiyana
7,836 Pointsef first_4(carlton): return carlton[:4]
def first_and_last_4(carlton): return carlton[:4] + carlton[-4:]
def odds(carlton): return carlton[1::2]
Manu Febie
1,754 PointsWhy 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]
Christopher Kehl
18,180 Points90% of the treehouse challenges are way off the subjects. horrible!!
Justin Carlson
12,755 PointsJustin 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
1,686 PointsSimon Klit
1,686 PointsGreat. Welcome!