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

Can some one tell me how to solve task 4 in 'SLICE FUNCTIONS' task

I am not sure about what should be returned in the last function

def first_4(my_iterable):
    return my_iterable[0:4]

def first_and_last_4(my_iterable2):
    join = my_iterable2[0:4] + my_iterable2[len(my_iterable2)-4:]
    return join

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

def reverse_evens(my_iterable4):
    return my_iterable4[2::]

2 Answers

Vidhya Sagar
Vidhya Sagar
1,568 Points

The output for you're code is the even indexes will be returned in the actual order. But the challenge is asking for the reverse of it .So save the actual order in a variable and return the inverse of the variable by using [::-1] on it . Try it on you're own. Ive added my code for reference.


def reverse_evens(my_iterable4):
    return a[::-1]

Actually, your code sort of works, but it won't work if the list's length is an odd amount.

Vidhya Sagar
Vidhya Sagar
1,568 Points

Why are u saying it does not work for lists with odd length ? I did try it and it works fine.

Nevermind. I thought I read somewhere else that this won't work with an odd amount of elements.

jonathan chadeyras
jonathan chadeyras
15,015 Points


I post that here because I think it's somehow related.

I tried that and it works in my terminal but not on the website:

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

any one has an idea why?