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

def reverse_evens(arr): return arr[::-2] This code works well in PyCharm, but it is not being accepted here. Why?

This is task 4 of the challenge concerning slices in Python. The objective is to create a function that returns the even indexes in reverse order.

def first_4(arr):
    return arr[0:4]

def first_and_last_4(arr):
    return arr[0:4] + arr[-4:]

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

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

2 Answers

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Try testing on an even length list: [1, 2, 3, 4, 5, 6]

Simply stepping backwards from the end by 2 is insufficient. Try slicing the even indexes then reversing the results.

Post back if you need more help. Good luck!

Thank you! This is exactly what i needed!

Steven Parker
Steven Parker
229,732 Points

:warning: Be careful about testing a challenge in an external REPL.

If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.

This approach will only work on lists that have an odd number of items. If the list passed to the function has an even number of items, this will return reverse odds instead of reverse evens.

You'll either need to calculate the starting position based on the list size, or you'll need to extract the evens first and then reverse them in a separate step.

Thank you for your help! I did a terrible job explaining my question. The function is supposed to return the even INDEXES in reverse order, not necessarily even values. For example, if I pass the list [1, 2, 3, 4, 5] in my function, it should return [5, 3, 1]. I'll do a better job at explaining my questions next time. Thank you again!

Steven Parker
Steven Parker
229,732 Points

It was clear you wanted the even indexes.