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

Ionut Dragan
Ionut Dragan
1,696 Points

Task 4 of 4 slices.py: Didn't get the right values from `reverse_evens`.

Hello there.

Ive tested this on my terminal and I got the right values from the reverse_evens function. Dont understand what is the problem there.

Here is my code:

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

def last_4(value):
    return value[-4:]


def first_and_last_4(value):
    return first_4(value) + last_4(value)


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

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

1 Answer

Ionut Dragan
Ionut Dragan
1,696 Points

Solved with:

def reverse(value):
    return value[::2][::-1]

A little late responding, but that is perfect! First you get every even index with [::2] and reverse it with [::-1]. This is exactly why Python is my favorite language. without having to iterate through an array you can get all values that are located at an even index. In C you would have to set a variable to iterate through a for loop and output each value if the index%2 ==0 and blah blah. Guido van Rossum is a God! or is he God? Python is the only Miracle I have ever seen.