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

Need help with slices

If you would direct your attenton to my code...

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

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

def first_and_last_4(watcha):
    return watcha[:5:-1:-5]

1 Answer

On the last step, your problem comes from the way you're trying to implement it. Since you're returning two different sections of the list you're splicing, you'll need to call one for the first set, and one for the second.

In slices, the first argument you enter is your starting point. The second is your ending point. The third, is your step, so [:5: -1:-5] means you want the first 6 items(Remember, indexes start at 0), and that you'd like to get to -1, but by taking away 5 each time.

Once you've called the first four, you can add(+) it to the first one, and just splice the negative in the following set, so that it places them together.

def first_4(iterable):
    return iterable[:4]

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

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

Thank you very much and thank you even more for expaining what I did wrong not just how to fix it.