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!

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

reverse_evens in slice-functions: Does anyone know why I'm stuck?

Does anyone know why I'm stuck? This is Challenge Task 4 of 4

If I do this, it works just fine in the console:

x=[1,2,3,4,5] x[::-2] x[-1::-2] # This returns an equivalent answer [5,3,1] # Example answer

Though the question is asking for reverse_evens, it's asking for elements in the even-numbered indices (which happen to be odd numbers in the example). I say this because I was wondering if my interpretation of the question is incorrect -- or is the example incorrect?


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):
    return iterable[::-2]

1 Answer

Viraj Deshaval
Viraj Deshaval
4,874 Points

You are super close to the solution. Your solution works in some cases and do not work in other cases. For example if iterable = [1,2,3,4,5,6]. Idea here is to first take the even indexes and then reverse them. Hope this helps!

Wow, thank you very much Viraj! :)