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

Jay See
Jay See
6,880 Points

Any help on reverse evens?

Works in atom. Yes I know shouldn't use txt editor but no output here to debug so I cannot see why it is failing here.

slices.py
def reverse_evens(arg):
    nums = arg[:]
    if (nums[-1] % 2) == 0:
        return nums[::-2]
    else:
        return nums[-2::-2]

def odds(args):
    odds = args[:]
    return odds[1::2]

def first_4(arg):
    first = arg[:]
    return first[0:4]

def first_and_last_4(arg):
    first = arg[:]
    last = arg[:]
    return first[0:4] + last[-4:]

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher
def reverse_evens(arg):
    nums = arg[:]
    if (nums[-1] % 2) == 0:
        return nums[::-2]
    else:
        return nums[-2::-2]

Why are you checking if the last number is even or not?

Jay See
Jay See
6,880 Points

I went from the position of thinking "I don't know whether the last number is even or not". So with that I worked literally in reverse and allowed the function to know if the last number was even or not to find a start point of where to reverse from.

I know it is a really inefficient and verbose route from looking at others solutions, I am just keen to know why it didn't pass?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

The challenge is to give a reversed list of items with an even index. The value of the item doesn't matter at all.