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 trialChandelor Simon
2,242 PointsHow does one return first and last four items in iterable?
#intended to return first and last four items in iterable
#said I got it right, then said it was wrong in next step
#I didn't change anything between steps
#what's wrong here?
def first_and_last_4(whatever):
return whatever[:4] and whatever [-1:-4:-1]````
2 Answers
Steven Parker
231,275 PointsYou've got the right idea, but the concatenation operator to join strings is "+". The "and" operator is for logic expressions.
And you haven't quite got the right slice argments to get the "last 4". Remember the "keep it simple" principle.
Chandelor Simon
2,242 PointsMan....
So obvious now - and I don't know how I didn't see that.
Weird - I hadn't tried that yet; I don't know how I got to the subsequent steps! Thank you.
Chandelor Simon
2,242 PointsChandelor Simon
2,242 Pointsah - I actually tried this at one point haha; must be how I got to next step somehow. Thank you once again, Steven. You've saved me from multiple headaches now.
Chandelor Simon
2,242 PointsChandelor Simon
2,242 Pointshits brakes okay wait -
S: You haven't quite got the right slice arg[u]ments to get the "last 4". Remember the "keep it simple" principle. Winky face C: Okay, tried multiple things here, and I must be close or have had it because I did go through again and actually got to the last step. Here's how my brain worked this:
Okay, firstly: -1 through -4 is not right because that doesn't return all the members I need. I actually need through -5 because I need four members and it isn't starting at index 0 like at the beginning.
Keep it simple... alright...
A. maybe I don't need to specify that it starts at -1 if I say it's through -4 and going backwards (::-1)? Not that.
B. Okay... maybe it's still trying to return two separate things and that's stressing it out; put it all in parenthesis? Nope.
C. Allllright - well maybe I can just concatenate without specifying that it's "whatever" again (return whatever [:4 + -1:-5:-1] Nope.
D. Alright - same as C, but keep the first and last in their own brackets (return whatever[:4] + [-1:-5:-1]) Nay nay.
Not that I have a lot of points behind me yet - but dang: never been this stuck before. Been reviewing my notes and I still can't seem to figure it out. Any additional guidance would be pretty sweet.
Steven Parker
231,275 PointsSteven Parker
231,275 PointsOkay, so you don't want to reverse anything here, just get the last 4 items. So the simple way is to just begin the slice starting at the 4th item from the last (-4). Other arguments aren't needed since by default the slice will go to the end.
The last 4 would then be
whatever[-4:]
.And note that you must also repeat the variable name, you can't just put an extra set of slice arguments in braces.