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 python

so i believe I'm starting at the negative index at stepping back -2 for every positive index. whats going wrong?

slices.py
def first_4(thing):
    return thing[0:4]
def first_and_last_4(thing):
    return thing[0:4] + thing[-4:]
def odds(stuff):
    return stuff[1::2]
def reverse_evens(thing2):
    return thing2[-1::-2]
Todd Anderson
Todd Anderson
4,260 Points

Hi

You have to check that you are grabbing the last even index which you can with a variable like

a = thing2[::2] and then return it in reverse like return a[::-1]

If you just take the last index it will only work when the index at the end is even like in the example problem, but if the 5th index was the last index it wouldn't work. The way I have described it will only grab the last even index. I hope this makes sense.

1 Answer

Steven Parker
Steven Parker
231,007 Points

If you start picking values from the end of the list and work backwards, whether they are "evens" or "odds" will depend on the length of the list and which one you start on. So you could compute the starting position based on the list size.

Another approach would be to pick out the even indexed values first and then reverse them in a separate step.