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

gibran erlangga
gibran erlangga
5,539 Points

take first 4 and last 4 items in a variable

I'm trying to solve this problem.

When I tried in terminal (I use mac), It works to the desired outcome, but the "bummer" words keeps coming to me. Anyone can take a look to help me with this? Thanks!

slices.py
def first_4(iterab):
    return iterab[:4]
def odds(iterab):
    return iterab[1::2]
def first_and_last_4(iterab):
    a = iterab[:4]
    b = iterab[-4:]
    a.extend[b]
    return a

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi again gibran erlangga ! You're still doing great, and you won't believe how close you are. Your code is functional, but you're implementing it in a way they aren't expecting. I'm not sure if this is because they are sending in an iterable that doesn't use the extend() method or not. It's possible. Remember they just said "iterable". They never said what kind. If i modify your code only slightly, it passes:

def first_and_last_4(iterab):
    a = iterab[:4]
    b = iterab[-4:]
    return a + b

However, you could do the same thing a little more directly like this:

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

Hope this helps! :sparkles:

gibran erlangga
gibran erlangga
5,539 Points

Hi Jennifer Nordell! youve been my saviour for my last 2 questions! To be honest, your fast and sharp response mean so much to my learning process. Thanks a lot!