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

Why does this not go in reverse evens?

For some reason this does not work in challenges, while it works in workspaces. I am reversing in evens. It starts from -1th index and then increments by -2 making it go backwards

slices.py
def reverse_evens(item):
    list(item)
    return item[-1: :-2]

People online have said to use the following, it works... I just don't know how and why because my solution seems perfectly fine as well..

return item[::2] and item[::-1]

2 Answers

Dear Marie,

did you try? print(reverse_evens("hello")) It should be olleh your code gives => olh

might this will help you to solve the problem. https://stackoverflow.com/questions/509211/explain-slice-notation

Greetings Julian

And => return item[::2] and item[::-1] works but is not 100% "correct" one part is unnecessary

Thanks Julian Steffen However, I'm still not able to figure out why my code went wrong.. it starts from -1 and goes by backwards in two increments.. this worked in workspaces! How is going two increments and then -1 going to make it go even and that too reverse?

Dear Marie,

the Problem is by your solution.

def reverse_evens(item): list(item) return item[-1: :-2]

print(reverse_evens([1,2,3,4,5])) [5, 3, 1] print(reverse_evens([1,2,3,4,5,6])) [6, 4, 2]

your solution can't handle list with even length. Might you have now a point to think about it again.

Greetings Julian