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 trialmamadou diene
Python Web Development Techdegree Student 1,971 PointsYou'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
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
Treehouse Moderator 68,441 PointsHint: 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!!
mamadou diene
Python Web Development Techdegree Student 1,971 Pointsi 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
Python Web Development Techdegree Student 7,496 PointsI 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
Treehouse Moderator 68,441 PointsIf 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
mamadou diene
Python Web Development Techdegree Student 1,971 Pointsi 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
Treehouse Moderator 68,441 PointsGet evens using
evens = lovecoding[::2]
Then return the reverse: evens[::-1]
mamadou diene
Python Web Development Techdegree Student 1,971 Pointsi feel like a dummy.. Thank you it worked
mamadou diene
Python Web Development Techdegree Student 1,971 Pointsmamadou diene
Python Web Development Techdegree Student 1,971 Pointsthank u for your pointer: i m still not passing. Here is what i've done
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsAnother hint: create evens similarly to how odds were created using [::2] then reverse this result.