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

question on... i ssem to have a correct output but getting a an evaluation as incorrect... requesting for assistance...

You're on fire! Last one and it is, of course, the hardest.

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].

You can do it!

my code:

list_1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] list_even = [1, 2, 3, 4, 5, 6, 7]

def first_4(list_0): first_list = list_0[:4] return first_list

def first_and_last_4(list_0): first_list = list_0[:4] second_list = list_0[-4:] first_list.extend(second_list) return first_list

def odds(list_0): first_list = list_0[1::2] return first_list

def reverse_evens(list_0): first_list = list_0[::-2] return first_list

list = first_4(list_1) list2 = first_and_last_4(list_1) list3 = odds(list_1) list4 = reverse_evens(list_even)

print(list) print(list2) print(list3) print(list4)

output: [0, 1, 2, 3] [0, 1, 2, 3, 16, 17, 18, 19] [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] [7, 5, 3, 1]

slices.py
list_1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
list_even = [1, 2, 3, 4, 5, 6, 7]

def first_4(list_0):
    first_list = list_0[:4]
    return first_list

def first_and_last_4(list_0):
    first_list = list_0[:4]
    second_list = list_0[-4:]
    first_list.extend(second_list)
    return first_list

def odds(list_0):
    first_list = list_0[1::2]
    return first_list

def reverse_evens(list_0):
    first_list = list_0[::-2]
    return first_list



list = first_4(list_1)
list2 = first_and_last_4(list_1)
list3 = odds(list_1)
list4 = reverse_evens(list_even)

print(list)
print(list2)
print(list3)
print(list4)

output: [0, 1, 2, 3] [0, 1, 2, 3, 16, 17, 18, 19] [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] [7, 5, 3, 1]

5 Answers

Kent Åsvang
Kent Åsvang
18,823 Points

I think the problem is that you are returning the evens in reverse without considering the length of the iterable. You should return the even indexes in reverse. (if I have understood the challenge correctly)

To achieve this you just check the length of the list. If the length of the list is an odd number, you can use the slice-method you already have, but if the length of the list is even you have to omit the last item first.

Hope this helped.

it worked.. thanks you very much for your help

Kent Åsvang
Kent Åsvang
18,823 Points

No worries. Glad to help!

thanks for the help... are you able to solve the quiz?

Kent Åsvang
Kent Åsvang
18,823 Points

You can try this:

    def reverse_evens(iterable):
        evens = iterable[0::2]
        rev_evens = evens[::-1]
        return rev_evens

I do not have access to the challenge so I can't verify it.

Shefeek N
Shefeek N
1,303 Points

Anything wrong with this??

def reverse_evens(my_list):
        return my_list[0::-2]
Kent Åsvang
Kent Åsvang
18,823 Points

Yes. While that function would work if the list has an even numbered length, f.eks:

    nlist = [1, 2, 3, 4, 5, 6, 7, 8]
    nlist[::-2]  # outputs [8, 6, 4, 2]

your function would break down when the length of the list is an odd number:

    nlist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    nlist[::-2]  # ouputs [9, 7, 5, 3, 1]

the problem occurs because the slicing starts at the end, while you want the evens from the start. Hope that makes sense

You should consider the length of the interable.

def reverse_evens(x):

    return x[0::2][::-1]