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

Make a function named reverse_evens that accepts a single i

this works perfectly in work spaces but i cannot pass the challenge! am i missing something...

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

def first_and_last_4(morestuffhere):
    return morestuffhere[0:4] + morestuffhere[-4:]

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

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

1 Answer

Wesley Trayer
Wesley Trayer
13,812 Points

The mistake is easy to make. "reverse_evens" assumes the last item in the iterable is in an even position, but what if it is odd?

a = ['a', 'b', 'c', 'd', 'e', 'f', 'g']  # The last item is in position 6, so your code returns the even items
b = ['a', 'b', 'c', 'd', 'e', 'f']  # The last item is in position 5, so your code returns the odd items

So you first need to get the even items starting from the beginning, then reverse their order.

Hope this helps!