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

Todd Bascombe
seal-mask
.a{fill-rule:evenodd;}techdegree
Todd Bascombe
Full Stack JavaScript Techdegree Student 11,133 Points

I test my code and it shows I was correct, why am I getting a wrong on this task

can someone take a look

slices.py
def first_4(lst):

    return lst[:4]

def first_and_last_4(lst2):
    lst21 = lst2[:4]
    lst3 = lst2[-4:]
    lst2 = lst21 + lst3
    return lst2

def odds(items):

    return items[1::2]

def reverse_evens(item):

    return item[-1::-2]

2 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hey Todd,

It'll work if you have [1, 2, 3, 4, 5], but it will fail if you have [1, 2, 3, 4, 5, 6]. That's why the last one of course, is "the hardest."
There are a few way to do this. You could check the length of the iterable using an if statement and use the appropriate code on whether it's even or not. But, I think the DRYest way is to get all the even numbers from the iterable first, and then reverse the order.

If you need to, you can have a look at this post, where Jeff does provide the answer. Give it a shot first with the above in mind, then if you are still stuck, you can look at Jeff's answer.

Keep Coding! :) :dizzy: