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

reverse_evens() works on workspaces but doesn't on the challenge website.

I am on the last part of the coding challenge for slices, and the function should return all the even index's backward. When I test my code in the workspace it does just that, but on the challenge website, the only error that I get is that the answer is not right. Any help?

slices.py
def first_4(iterable):
    slice = iterable[:4]
    return slice

def first_and_last_4(iterable):
    slice = iterable[:4] + iterable[-4:]
    return slice

def odds(iterable):
    slice = iterable[1::2]
    return slice

def reverse_evens(iterable):
    slice = iterable[::-2]
    return slice

1 Answer

Istvan Nonn
Istvan Nonn
2,092 Points

I have the same problem in the last part. I tried different approaches like:

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

or

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

or

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

or even excluding 0(zero)

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

For sure the answer is in front of me, just can't see because is a simple solution.

Istvan Nonn
Istvan Nonn
2,092 Points

I made it ... Thanks to Jennifer Nordell

Hi there! Yes, your code works, but only in half cases. Notably, it works when the number of elements is odd. So the given the test data [1, 2, 3, 4, 5], it will return [5, 3, 1]. But if we put in [1, 2, 3, 4, 5, 6], it will return [6, 4, 2], which is not what we would expect. We would still expect to get back [5, 3, 1] as the even indexes still contain 1, 3, and 5.

You will have to do this a bit differently. First, get all the even indexes, then reverse them.

I think maybe you can get it with this hint, but let me know if you're still stuck! :sparkles:

Please read carefully and you can do it! I am not going to give just yet the final answer.