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

Luc De Schepper
Luc De Schepper
4,026 Points

reverse_evens error message even when I return correct values

Here's my function for reverse evens:

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

When I use my pycharm IDE to run this code with a list of [1, 2, 3, 4, 5] I get a return of [5, 3, 1] which is identical to your example.
However, I get an error message saying the values for reverse_evens are not correct.

Is there something wrong with my code, or something wrong with what the expected return values are supposed to be? Wracking my brain here, and I can't figure it out.

Thanks,

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

def first_and_last_4(y):
    first = y[:4]
    last = y[-4::]
    return first + last

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

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

3 Answers

nicole lumpkin
PLUS
nicole lumpkin
Courses Plus Student 5,328 Points

Yes but the code below evaluates to a list that contains an odd number of elements. Your code is successful there but fails with lists containing an even number or elements.

my_list = [1,2,3,4,5]

len(my_list)

Try running your code with this

my_list = [1,2,3,4]
Luc De Schepper
Luc De Schepper
4,026 Points

I see what you were saying. My bad. If the list itself was of an even number of objects, my code fails. I was able to cobble together some code that finally worked. Thank you so much for your help.

nicole lumpkin
nicole lumpkin
Courses Plus Student 5,328 Points

Glad you got it! I really struggled with this one too :)

nicole lumpkin
PLUS
nicole lumpkin
Courses Plus Student 5,328 Points

Hello Luc!

Try running your function with a list whose length is even.

reverse_evens([1,2,3,4])

Your output should be the following:

[4,2]

Uh-oh, the challenge is to return elements with even indices but 4's index is three, and 2's index is 1!!!

Luc De Schepper
Luc De Schepper
4,026 Points

Hi Nicole,

Yeah, that's what I found confusing. The literal example of what should be returned in the challenge are the numbers found in the even index values of the list, which are [5, 3, 1]. That's what I got to return with my code, and I still get the error.

I'm even trying:

my_list = [1, 2, 3, 4, 5]

my_list[-4::2]   # [2, 4]
my_list[3::-2]   # [4, 2]
my_list[5::-2]   # [5, 3, 1]
my_list[-5::2]   # [1, 3, 5]

None of it works. I can't complete the challenge. Thank you for your help though. I think this is just a bug in the quiz.

Best,

Luc

Minwook Jeong
Minwook Jeong
2,453 Points

I've just got that idea from other post

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

It's related with length of list but I am not sure why length wasnt applied to Task 2/4 3/4...