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

slices.py

even items

slices.py
def first_4(iterable_thing):
    return iterable_thing[0:4]
def first_and_last_4(single_iterable):
    return single_iterable[0:4] + single_iterable[-4:]
def odds(odd_iterable):
    return odd_iterable[1:-1:2]
def reverse_evens(item):
    return item[-2::-2]

Hi James, here is the question; You're on fire! last one and it is of course the hardest. Make a single 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] would return [5,3,1]

2 Answers

First two function are correct, i dont understand how your code passed correctly but I think your 3rd functions has some problem as well. For 4th function, you have to find the even elements and then return the reverse of it.

!Code mentioned below, if you want to try on your own first... stop reading here.

.

.

.

.

.

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

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

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

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

i just completed challenge with the help of the last line of code, but how does that even work. I tried it out in the Console and it reverses it and prints every other number starting with an odd number, so 9, 7, 5, 3, 1 and we are supposed to be getting evens!

John Vieyra
John Vieyra
2,826 Points

I think it's because once you establish your evens in the first list. The second list decrements the first list which is set to only move in 2's.