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

Chandelor Simon
Chandelor Simon
2,242 Points

How 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
Steven Parker
231,275 Points

You'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. :wink:

Chandelor Simon
Chandelor Simon
2,242 Points

ah - 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
Chandelor Simon
2,242 Points

hits 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:

  1. 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.

  2. 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
Steven Parker
231,275 Points

Okay, 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 :point_right: 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.

Chandelor Simon
Chandelor Simon
2,242 Points

Man....

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.