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

"Didn't get the right values for 'reverse_evens'"... but I do when I run it in console?


I'm very confused as to why my answer here is not being accepted. When I run reverse_evens() in PyCharm, it returns [5, 3, 1] like it says it should. However when I try to submit it says it's not getting the right values. Can anyone help?

def first_4(iterable):
    return iterable[:4]

def first_and_last_4(iterable):
    return iterable[:4] + iterable [-4:]

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

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

2 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there! Your code does work but only in certain cases. Namely, it only works when the number of elements in the array is odd. Take a look at what happens when you do reverse_evens([1, 2, 3, 4, 5, 6]). I feel like you can get it now that you know why you might not be getting the correct result.

Hope this helps! :sparkles:

Ah great point! I figured it out with an if statement, thank you!