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 trialNathan Tallack
22,160 PointsShould work returning reverse stepped list.
The last task asks us to make a 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] as the input, the function would return [5, 3, 1]. Here is my code.
def reverse_evens(i):
return i[-1::-2]
But it keeps telling me it didn't get the right values from reverse_evens
even though I am pretty sure my code should give the expected result.
def first_4(i):
return i[:4]
def first_and_last_4(i):
return i[:4] + i[-4:]
def odds(i):
return i[1::2]
def reverse_evens(i):
return i[-1::-2]
3 Answers
Steven Parker
231,269 PointsYou're halfway there. Your function will return the reverse evens if the list has an odd number of elements. But otherwise it will return "reverse odds" instead.
To always return reverse evens, you'll need to compute the starting position based on the list size, or extract the even indexes first and then reverse them in a separate step.
Thomas Bråten
1,408 PointsI've been going over this over and over again and I just can't get it in my brain. Looking at different examples i see almost the same explanations but I just dont get it. So, if I understand it correctly have an if to check if length of string that is passed in is odd or even. If odd make, this list else make other. Even writing this down just spins my head. Could someone post a working example of the last challenge? And if it can be explained as well. Its something I just don't get and I don't even get what I don't get. I'm sure I'll have a facepalm at the end of this.
Steven Parker
231,269 PointsThe simplest approach is to extract the even indexes first and then reverse them with a separate slice:
def reverse_evens(i):
return i[::2][::-1]
Thomas Bråten
1,408 PointsThank you Steven, I'm gonna go through it a couple times. Sometimes I need to see it different ways :)
Nathan Tallack
22,160 PointsNathan Tallack
22,160 PointsAh, see, I was working to only satisfy the example, not all possible cases. My bad. Thanks for that. :)