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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 35,157 Points

Code challenge stage 3 - first and last 4

Following on my last question, logical thinking doesn't solve this problem. I've tried:

return iterable[0:4] + iterable[-5:-1]

which doesn't get me the last 4 numbers - I fall one short. but when I do this:

return iterable[0:4] + iterable[-4:0]

It doesn't add anything so I just get the first 4.

If I can't use index 0 as my stop point, I don't understand how to get the actual last 4 numbers. This really isn't logical logic. I would appreciate help. Sorry. I always try about a billion different things before I resort to this.

Nnacy M.

slices.py
def first_4(iterable):
  return iterable[0:4]

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

def first_and_last_4(iterable):
  return iterable[0:4] + iterable[-5:0]
Nancy Melucci
Nancy Melucci
Courses Plus Student 35,157 Points

Ha ha 5 minutes after I posted this it came to me...YAY.

thanks and sorry....

NJM

1 Answer

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

0 is the first item. So [-5:0] doesn't make sense without a negative step as well. Just leave off the stop argument and it'll go all the way through the end.