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 trialBen Hedgepeth
Python Web Development Techdegree Student 10,287 PointsSlicing even indexes in a list
Please refer to the reverse_evens
function. I wrote an if statement to evaluate whether the length of the iterable is even or odd. If it length of the iterable happens to be even the last index will be odd; and I want to start removing indexes from position -2 and iterate -2 steps.
For iterables that have an odd length, I want to remove the even indexes starting at position -1 (because the last index is even) and iterate -2 steps.
def reverse_evens(iterable):
if len(iterable) % 2:
# the last index of an iterable that happens to have an even length is odd
return iterable[-2::-2]
else:
# the last index of an iterable that happens to have an odd length is even
return iterable[-1::-2]
1 Answer
Steven Parker
231,269 PointsYour concept is sound, but the implementation has the cases reversed. When the length is odd, the starting offset should be the last item (-1). And when the length is even, the offset should be the second-to-last item (-2).