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

Ian Cole
Ian Cole
454 Points

The slice function works, but doesn't work

I hammered out this piece and it all works except the last function, "reverse_evens". It tells me it's not returning the proper value. So I took it to my interpreter and... The output is perfect. It's exactly what it should be.

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

def first_and_last_4(itr):
    first_4 = itr[:4]
    last_4 = itr[-4:]
    first_and_last_4 = first_4 + last_4
    return first_and_last_4

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

def reverse_evens(itr):
    reverse_evens = itr[-1::-2]
    return reverse_evens

1 Answer

Your solution for reverse_evens() would only work for certain inputs. Can you think of an input for which it will fail? Are you assuming the input to be of a specific size?

Can you divide the final task into 2 components rather than trying to achieve it all in one step?

Here's a hint: 1) Get all the even indexed values 2) Reverse the result from step 1 3) Return this reversed list

Let me know if need help with this. By the way, there's a neat little trick you can use with slices to reverse any list.

Ian Cole
Ian Cole
454 Points

Damn you're good, thanks for the help!