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

Reverse evens exercise is driving me crazy!

          def first_4(something):
    return something[0:4]
def first_and_last_4(something):
    return something[:4] + something[-4:]
def odds(something):
    return something[1::2]

def reverse_evens(something):
    if something[0]%2 =0:
        evens = something[::3]
        return evens [::-1]
    else:
        odds= something[::2]
        return odds [::-1]

The last function is causing me troubles, I have to return a evens list backwards, but I don't know why is not working.

2 Answers

Hello, I remember this function, it was hard. however after reviewing some solutions ppl mentioned that u also need the numbers to be evens the solution is:

def reverse_evens(something):
    return something[::2][::-1]

First,we want to step 2 indexes each time, so for example if we start at index zero, we step to index 2 and thats how we always in a even index. Second, we now want to reverse the output so we simply use the [::-1]

hope un understand ;]

So helpful! Thanks a lot