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

Error in Setep 4

Hi, I have a question. Why def reverse_evens(let): return (let[::-2])

return Error? If this is a bug or a future.

Best reqards Lukas

slices.py
def first_4 (let):
    return (let[0:4])

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


def odds (let):
    return (let[1:3])

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

3 Answers

Efaz Khan
Efaz Khan
5,194 Points

Hi, there is a useful function in python called reversed(). Try using that one

def reverse_evens(item):
    return list(reversed(item[::2]))

I know that, but my question was why this function return error? def reverse_evens(let): return (let[::-2])

I tested it in local pycharm and I do not have this problem.

Kailash Seshadri
Kailash Seshadri
3,087 Points

I tried the same thing, even in workspaces and got the correct output, but it isnt being accepted in the challenge:

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

I tried both item [::-2] and item [-1::-2] with it being rejected by the challenge but it works fine in workspaces.

JongHo Kim
JongHo Kim
8,313 Points

I believe I found out the issue. I had the same problem too, but then I realized that if the number of indexes was odd, counting backwards by two would return the values of the odd indexes. I chose to create a list of the even indexes and then reversed it to avoid this issue.

https://w.trhou.se/68zvs4nihh

My solutions are in question_answers.py

Hope this helps!