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

ido sh
ido sh
3,926 Points

I do the right thing but it say that I'm wrong

that is the task: You're on fire! Last one and it is, of course, the hardest. Make a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable with an even index...in reverse. For example, with [1, 2, 3, 4, 5] as the input, the function would return [5, 3, 1]. You can do it!

the answer: def reverse_evens(i): return i[::-2]

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

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

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

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

1 Answer

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you are returning every other index in reverse, not necessarily every even index in reverse. if the last index in a list is odd, you are returning the odds with [::-2]. either slice the evens from the beginning, then reverse, or cut off the last index of the list if it is an odd index to use [::-2].