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

You're on fire! Last one and it is, of course, the hardest. Make a function named reverse_evens that accepts a single i

i have been stuck in this challenge and don't seem to understand it

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

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

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

def reverse_evens(lovecoding):
    return lovecoding[-1:-10:-2]

5 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

Hint: sometimes you have to use two steps: Get the even indices, then reverse it.

Second Hint: you can stack slices (aka slice of a slice) to do this in one line.

Post back if you need more hints. Good luck!!

thank u for your pointer: i m still not passing. Here is what i've done

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

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

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

def reverse_evens(lovecoding):
    evens = [2,4,6,8]
    return lovecoding(evens=[-1::-1])
Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

Another hint: create evens similarly to how odds were created using [::2] then reverse this result.

i made more corrections here

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

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

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

def reverse_evens(lovecoding):
    evens = lovecoding[2,4,6,8]
    return evens[-1::-1]

[MOD: added ```python formatting -cf]

Nik Omel
seal-mask
.a{fill-rule:evenodd;}techdegree
Nik Omel
Python Web Development Techdegree Student 7,496 Points

I have some issue:

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

I tried in console with our example and I got the right result. here is my code -- >

items = [1, 2, 3, 4, 5]

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

print(reverse_evens(items))
[5, 3, 1]

please help

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

If you try reverse_evens([1, 2, 3, 4, 5, 6]) you will not get the correct answer. Use two steps: get evens, then reverse them

i have created a list of even indexes, then i took that list and return it in reverse and still getting it wrong

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

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

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

def reverse_evens(lovecoding):
    even_index = lovecoding = [2, 4, 6, 8, 10]
    return reverse_index[-1::-1]

[MOD: added ```python formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

Get evens using

evens = lovecoding[::2]

Then return the reverse: evens[::-1]

i feel like a dummy.. Thank you it worked