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

tyler bucasas
tyler bucasas
2,453 Points

slices.py

when I put the reverse_evens in the python shell it returns the right numbers, but when I check the question is says its not receiving the right numbers. someone please help

slices.py
def first_4(list):
    return(list[:4])

def first_and_last_4(list):
    return(list[:4]) + (list[-4:])

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

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

1 Answer

What I did was to first, create a new variable which is a list of all the even numbers. Then I returned that list in reverse. There may be a way to do this all on one line, however. But this worked for me.

def reverse_evens(list):
    list_v2 = list[0::2]
    return(list_v2[::-1])

Actually, I just tried it out on one line:

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

So I'm doing the exact same thing - getting a list of evens and then reversing that list.

In your code, you were only reversing the list by an increment of 2. That would only work if the list of numbers were odd (that is, the last index was an even number).