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
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,060 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.

My solutions are in

Hope this helps!