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 trial

Python Python Collections (2016, retired 2019) Slices Slice Functions

Antoine Solomon
Antoine Solomon
2,850 Points

My implementation of the reverse_evens() doesn't seem to work. Could you review my function ?

def reverse_evens(list): return list[::-2]

slices.py
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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Hi 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
Antoine Solomon
2,850 Points

Looking 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
Antoine Solomon
2,850 Points

Actually i retested my solution and it didn't work. :-(

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Oops :(. 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.