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 trialNicholas Rogers
8,384 PointsChallenge Task 4 of 4 Return reverse_evens
Can't seem to get it right iterable[::-2] and iterable[-1::-2] don't work. neither does : evens = iterable[::2] evens.sort(reverse=True) return evens
def first_4(thing):
return thing[:4]
def first_and_last_4(thing):
return thing[:4] + thing[-4:]
def odds(thing):
return thing[1::2]
def reverse_evens(thing):
evens = thing[::2]
evens.sort(reverse=True)
return evens
2 Answers
Stuart Wright
41,120 PointsYour logic is correct, but your code fails the challenge because .sort() is a list method only, and there are other iterables such as strings, which cannot be sorted in place. Just change your syntax to reverse your evens iterable to something that would work for lists or strings, and your code will pass the challenge.
Hint: something similar to how you got the even index numbers will work.
Nicholas Rogers
8,384 PointsAh, I was mainly confused as to why iterable[::-2] wasn't working, but it looks like I just misunderstood the question: A reversed list of even indexed items vs a list of even indexed items from a reversed list. Thanks for the explanation regarding .sort().