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

What am I missing solving reverse_evens task using .sort(reverse=True)

I'm sure I am missing something simple here so my apologies if that is the case and thank you for taking a look.

In the Python Collections -> Slice Functions -> Task 4/4 where it asks you to create a reverse_evens function I tried the following:

def reverse_evens(my_iterable): backwards_evens = my_iterable[0::2] backwards_evens.sort(reverse=True) sorted_backwards = backwards_evens return sorted_backwards

I have created this as a .py script to test in workspaces and tested it without an issue. I noticed that earlier folks that ran into trouble did so by not factoring in whether the iterable was even or odd.

My hope was to workaround this issue by starting from 0, moving forward 2 steps and then, later in my script, reversing the oder using the order using the .sort function. In my tests this appears to work for iterables with both odd and even lengths.

I also tried testing sorted() as opposed to list.sort and seem to get the same working result. My guess is that there is a reason that my shortcut of using the sort function is not as foolproof as I think!

What else is not working the way it should?

Thank you very much for taking a look!

1 Answer

The challenge never asked you to sort the list then reverse the evens.

:point_right: You shouldn't sort the list at all!