Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Slices Slice Functions

i ran the example and a string, through my function and it work why won't it pass?

def reverse_evens(iterable):
    return iterable[::-2]
def first_4(iterable):
    return iterable[:4]

def first_and_last_4(iterable):
    first4 = iterable[:4]
    last4 = iterable[-4:]
    together = first4 + last4
    return together

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

def reverse_evens(iterable):
    return iterable[::-2]

2 Answers

Philip Schultz
Philip Schultz
11,413 Points

Hey, The reverse evens isn't working because you are assuming the length of the list is odd, what if the length of the list is even? It would start at an odd index then skip by 2, right. So, you can do this two ways. First, you can test the length to determine if its odd or even, then return a statement accordingly. Like so,

def reverse_evens(iterable):
    if len(iterable) % 2 == 0:
        return iterable[-2::-2]
        return iterable[::-2]
    return reverse_evens

Second, you can make a list of all evens going forward, then reverse. Like so,

def reverse_evens(iterable):
    even = iterable[::2]
    reverse_evens = even[::-1]
    return reverse_evens

!!!! its so awesome and frustrating realinzing the answer is so simple lol thank you