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!

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

Daniel Murray
Daniel Murray
3,294 Points

Could someone please explain why my function isn't correct?


On task 4 of Pyhton Collections challenge 'Slice Functions', the last function 'reverse_evens'. The code challenge is returning a 'Bummer: didn't get the right values from 'reverse_evens' error. But when I paste the code into the shell it returns the correct result.

The challenge is: "Make a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable with an even index...in reverse.

For example, with [1, 2, 3, 4, 5] as the input, the function would return [5, 3, 1]."




def first_4(iterable):

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

def odds(iterable):

def reverse_evens(iterable):

1 Answer

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there, Daniel Murray ! It looks like you're doing terrific and yes, your last function does work, but only sometimes. Notably, it works when the length of the iterable is an odd number. For instance, here, you send in [1, 2, 3, 4, 5] and get back [5, 3, 1], but if you send in [1, 2, 3, 4, 5, 6] you get back [6, 4, 2]. I would expect to still get back [5, 3, 1] as those are the numbers at the even indexes from the beginning.

Suggestion: try getting every other one first and then reversing the result.

Hope this helps! :sparkles:

Daniel Murray
Daniel Murray
3,294 Points

Ok got it, thank you Jennifer! Really helped. 😎