Welcome to the Treehouse Community
Looking to learn something new?
Daniel Mula TarancónPython Development Techdegree Student 16,031 Points
I have to return every item in the iterable with an even index in reverse
I've created a variable called reverse in which I've put the iterable in reverse position. Afterwards I tried to extract the even items but it seems to be that it is wrong and I don't see why.
def first_4 (iterable): return iterable [:4] def first_and_last_4 (iterable): return iterable [:4] + iterable [-4:] def odds (iterable): return iterable [1::2] def reverse_evens (iterable): reverse = iterable [::-1] return reverse [::2]
Jennifer NordellTreehouse Teacher
Hi there! You're super close here, but it's the order in which you're doing this that is messing things up a bit. Let's say for example that our test data is actually
[1, 2, 3, 4, 5, 6] instead of the one given. Your code first reverses that so you get
[6, 5, 4, 3, 2, 1]. Then it takes a slice containing every other element which would result in
[6, 4, 2]. But we want back the elements that were at even indexes when it started and in reverse. The result we should be getting is
[5, 3, 1].
The solution here is to reverse the order you have things. First take every other element, which even starting with [1, 2, 3, 4, 5, 6] would be [1, 3, 5] and then reverse those. Your code actually works in half the cases. It works when the iterable has an odd number of elements. But reversing the order will work regardless of the number of elements in the list.
Hope this helps!