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 trialJacqueline Bennie
1,020 PointsLast Step... Make a function named reverse_evens that can perform [1,2,3,4,5] into [5,3,1]
I feel like I'm so close but it's still not saying that I'm correct. What am I doing wrong on this last step?
def first_4(item):
return(item[0:4:1])
def first_and_last_4(item):
return(item[0:4] + item[-4:])
def odds(item):
return(item[1::2])
def reverse_evens(item):
return(item[-1::-2])
1 Answer
Steven Parker
231,269 PointsWithout giving any spoilers, I can tell you that there's two basic strategies for solving this challenge:
- you can compute a starting position using the length (actually the even/odd-ness) of the list
- you can extract even indexes first, and then reverse them with a separate slice operation
Either method will work when correctly implemented.
Joshua Esquer-Davis
9,415 PointsJoshua Esquer-Davis
9,415 PointsYou're solution will not work if the length is even you want to break this down into 2 steps one to move evenly across the list and the other to reverse the list.
item[::2][::-1]
will work for this.