Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Slices Slice Functions

Task 4 of the Code Challenge is... picky?


For the last task, 4 of 4, the auto-grader won't accept this solution:

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

However, running this locally works:

>>> a = list(range(1,6))                                                                                  
>>> a                                                                                                     
[1, 2, 3, 4, 5]                                                                                           
>>> a[-1::-2]                                                                                             
[5, 3, 1]

Any of you ran into this as well? Thanks!

1 Answer

Your code sort of works, but it only works for lists that have an odd length.

If the length of the list is even, you should doiter_item[0::-2]. However, as I said, that snippet only works for lists length even.

So, you would probably have to use an if/else statement to test if the length of the list is even or odd :smile:

Here's a hint:

To test if a number is even, you can do this:

Pretend I'm in the Shell

>>> a = 10
>>> a % 2 == 0  # This tests "is a even?", and it is so Python returns True
>>> b = a - 1  # In this case, b will be 9 (which is a - 1)
>>> b % 2 == 0

If you have questions on the % operator, respond below with the text #modoperator inside and I'll explain. :)

I hope this helps :grin:

Happy coding! :tada:

:dizzy: ~Alex :dizzy: