Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Slice Functions: Reverse_evens problem

Hi, I am not sure what the issue is with the last step. I made a small if statement to make sure if the last item is an even, then it would start at the second to last item (which should be odd). Then it would return odd numbers backwards, as asked in the problem.

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

def first_and_last_4(items):
    first = items[:4]
    last = items[-4:]
    both = first + last
    return both

def odds(items):
    return items[1:999:2]

def reverse_evens(items):
    if items[-1] % 2 == 0:
        return items[-2::-2]
    else:
        return items[::-2]

1 Answer

The list of numbers was just an example. But what if you try ["dog",2,3,4,5,"cat"]? The instructions state the function accepts a single iterable as an argument so that would be valid input. Instead of checking the value of a single item you could check the total number of items to see if it's odd or even.