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

Fergus Clare
Fergus Clare
12,120 Points

reverse_evens() Error with Sample Set (works correctly in terminal)

My function for reverse_evens() correctly returns the reversed list of even integers after running yet the challenge informs me that an incorrect response was returned. When I run the reverse_evens() function in terminal with the sample set of integers [1,2,3,4,5], I correctly receive a response of [4,2].

Any hints are appreciated.

slices.py
def first_4(x):
    return x[0:4:1]    

def first_and_last_4(x):
    n1 = x[:-5:-1]
    n2 = n1[:-5:-1]
    return (x[:4] + n2)

def odds(x):
    return x[1::2]

def reverse_evens(x):
    a = x[::2]
    for i in a:
        x.remove(i)
    return x[::-1]
Fergus Clare
Fergus Clare
12,120 Points

Is the error because I am modifying the original list of x instead of creating a new list with the reversed evens?

1 Answer

AJ Salmon
AJ Salmon
5,675 Points

Hey Fergus,

The issue here is that you're actually removing every number with an even index from the list. reverse_evens([1, 2, 3, 4]) should return [3, 1], not [4, 2]. You get the evenly indexed numbers with a = x[::2], and that's great! All you have to do after that is reverse and return a. Hope this helps, and happy coding! :)