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 function not working

My code is:

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

I tried this on an online compiler and on my own laptop. Both work. I dont know why it won't work here.

I dont know why it won't work even if it works elsewhere.

slices.py
def first_4(some_itr):
    return some_itr[:4]

def first_and_last_4(lst):
    return lst[:4] + lst[-4:]

def odds(itrable):
    return itrable[1::2]

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

1 Answer

Steven Parker
Steven Parker
231,275 Points

This strategy will work with some test data, but not every time. You managed to test with "lucky data".

To create a function that will work reliably with all data, there are two basic approaches:

  • compute which item to start with based on the size (or actually the "even or odd-ness") of the list, or
  • use one slice to extract all the even indexed values, and then another slice to reverse them

Either method will work when implemented correctly. Bonus hint: the second method might be easier to implement.

Thank you so much! I didnt think of that.