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 trialAntoine Solomon
2,850 PointsMy implementation of the reverse_evens() doesn't seem to work. Could you review my function ?
def reverse_evens(list): return list[::-2]
def first_4(list):
return list[:4]
def first_and_last_4(list):
first_4 = list[:4]
last_4 = list[-4:]
return first_4 + last_4
def odds(list):
return list[1::2]
def reverse_evens(list):
return list[-1::-2]
4 Answers
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsHi Antoine,
You just need to change the -1
to a -2
. Like so, [-2::-2]
. I tested this in the Python shell and it worked.
Python 2.7.14 (v2.7.14:84471935ed, Sep 16 2017, 20:19:30) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> iterable = [1,2,3,4,5,6,7,8,9]
>>> iterable[-2::-2]
[8, 6, 4, 2]
However, the code challenge didn't accept it, so I tried the more verbose code below and it passed.
def reverse_evens(iterable):
results = []
for i in range(0, len(iterable), 2):
results.append(iterable[i])
return results[::-1]
Let me know if you have any questions!
Antoine Solomon
2,850 PointsLooking at Chris Jones's answer I figured I could just use the list copy() method
def reverse_evens(list):
result = list.copy()
return result[-2::-2]
Antoine Solomon
2,850 PointsActually i retested my solution and it didn't work. :-(
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsOops :(. It could just be me, but I felt like the Python code challenges were the most picky of the languages I've learned through Treehouse. It seemed like most were looking for you to solve the challenge a specific way, rather than verifying your end result.
Antoine Solomon
2,850 PointsVery true.
Antoine Solomon
2,850 PointsAntoine Solomon
2,850 PointsHi Chris,
Thanks for commenting. I was going nuts with that particular issue.
Chris Jones
Java Web Development Techdegree Graduate 23,933 PointsChris Jones
Java Web Development Techdegree Graduate 23,933 PointsNot a problem! I'm glad you were able to use copy() to pass the challenge!