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

wayne ross
wayne ross
3,225 Points

Not sure why the reverse_evens() is graded wrong, all the test I do i get the output that the question ask for

reverse_evens should return the even indexes of the list in reverse order

slices.py
def first_4(list1):
    ret_value=list1[:4]
    return(ret_value)

def first_and_last_4(input_var):
    ret_value=input_var[:4]+input_var[-4:]
    return(ret_value)

def odds(list2):
    ret_value=list2[1::2]
    return(ret_value)

def reverse_evens(list3):
    rev_list=sorted(list3,reverse=True)
    ret_value=rev_list[::2]
    return(ret_value)

3 Answers

wayne ross
wayne ross
3,225 Points
def reverse_evens(var_4):
    ret_val=var_4[::2]
    ret_val.reverse()
    return(ret_val)
Steven Parker
Steven Parker
231,269 Points

:warning: An answer containing only explicit code that can be pasted into a challenge is strongly discouraged by Treehouse, and subject to redaction by staff and moderators.

But good job on finding a working solution!

FYI: this can also be solved using only slices.

for mine to work I had to do a length check. len(iterable)%2 == 0

based on the true or false I had slightly different evaluations for my slice syntax iterations. Hopefully that helps.

Steven Parker
Steven Parker
231,269 Points

There are two different approaches to this, one is to extract the even indexes first and then reverse the list (as done above), and the other is to calculate the starting point based on the size, as you did.

Steven Parker
Steven Parker
231,269 Points

I can see that this code will work on the example, but it is dependiing on the list having an odd number of elements and being in sorted order.

It needs to also work on arrays of different sizes and regardless of sorting.

Try using this as a test sample:
With ["try", "this", "as", "a", "test", "sample"] as the input, the function should return ["test, "as", "try"].

wayne ross
wayne ross
3,225 Points

def reverse_evens(var_4): ret_val=var_4[::2] sorted_ret_val=sorted(ret_val, reverse=True) return(sorted_ret_val)

Still didnt work, I thought about that after I sent the question so made some changes still not working

wayne ross
wayne ross
3,225 Points

def reverse_evens(var_4):

ret_val=var_4[::2]
sorted_ret_val=sorted(ret_val, reverse=True)
return(sorted_ret_val)
wayne ross
wayne ross
3,225 Points

Hey thanks for your input I just saw how to find my previous post. I will try this and let you know

wayne ross
wayne ross
3,225 Points

Hey Steven I just tried this and got out ['test','as','try']

wayne ross
wayne ross
3,225 Points

Steven now I remember what I did, I actually ended up using just slices. basically did a slice of the input [::2] then used the reverse slice option [::-1] for the list reversal

Steven Parker
Steven Parker
231,269 Points

Yes, that's the other method.

And normally, you'd choose "best answer" as the one that helped you the most, not on your own final result. :wink: