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 trialKevin Costa
2,199 Pointsreverse_evens challenge Bummer
def reverse_evens(lis): print lis[::-2]
reverse_evens([1, 2, 3, 4, 5])
Gives me the desired result in Repl.it but not in Treehouse.
def first_4(lis):
return lis[:4]
def first_and_last_4(lis):
return lis[:4:1] + lis[-4::1]
def odds(lis):
return lis[1::2]
def reverse_evens(lis):
return lis[::-2]
2 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointsslicing by [::-2] doesn't return evens, it returns every other element from the end, which may or may not be the even indices. you can either test for length and cut off the last element of the list if the last index is odd, then slice every other element, or slice every other element from the start (which is always even - 0), then reverse. you can chain slice operators like[:5][::-3] etc.
Kevin Costa
2,199 PointsAh that's helpful. SO I did " return lis[::2] " to get every other (aka even indexes startin from 0 --> 2) and then reversed the list. Thanks!!