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

Daniel Deiana
Daniel Deiana
11,341 Points

reverse_evens task

Hello I'm quite stuck and having a bad time with reverse step slices! So I've written some nasty code to try and get past the question but it's not passing which I'm not shocked about as I'm sure there is a very simple 2-3 line answer which I'm missing. I'm sure the if statement block passes but I figured I would need a different start point if the length of the iterable was modular zero. But as I say I'm having a bad time with reverse slices and I've probably missed the point somewhere. sorry for the bother

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

def first_and_last_4(item):
    return item[:4] + item[-4::1]

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

def reverse_evens(item):
    if len(item) % 2 == 1:
        return item[::-2]

    elif len(item) % 2 == 0:
        return_item = ""
        for character in item[::-1]:
            if not character in item[::-2]:
                return_item += character
        return return_item

2 Answers

Steven Parker
Steven Parker
231,269 Points

:point_right: Yes, you're working too hard. :smirk:

Your first two lines put you halfway there with the if test and returning the slice with a -2 step. Now you just need an else with a similar return, but this time using a start index that points the the item just before the last one.

I'll bet you can get it now.

Daniel Deiana
Daniel Deiana
11,341 Points

Thanks for your reply Steven, got it!!

Steven Parker
Steven Parker
231,269 Points

Glad to help!

Happy coding! :sparkles:

Hi Daniel,

Another approach you could take is to do a slice of a slice. The first slice gets the even indexes and the second slice reverses it. This will run a little slower but has the advantage of not having to worry about whether you have an odd or even length iterable.

def reverse_evens(iter1):
    return iter1[::2][::-1]
Daniel Deiana
Daniel Deiana
11,341 Points

Very clever! Thanks Jason

Daniel McFarlin
Daniel McFarlin
5,168 Points

Jason Anello, I just tried the way you put here and it didn't pass...unless there is typo here I'm not sure that will work :(

Did you type it out or copy/paste it?

I just ran through it again and copy/pasted it in and it passed for me.

Daniel McFarlin
Daniel McFarlin
5,168 Points

I guess there was space that got in there somehow lol cause I went back and did it again and it worked. I take back my comment.