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

Andrei Oprescu
Andrei Oprescu
9,547 Points

I have done this question correctly, but it said i'm wrong!

I have a question on a challenge that asks me this:

You're on fire! Last one and it is, of course, the hardest. Make a function named reverse_evens that accepts a single iterable as an argument. Return every item in the iterable with an even index...in reverse. For example, with [1, 2, 3, 4, 5] as the input, the function would return [5, 3, 1]. You can do it!

My code that I am using is at the bottom of the page. Is there something wrong with my code?

I have checked it also in the workspace and it gave correct answers.

can someone help me?

Thanks!

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

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

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

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

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You solution does work when the input list has an odd number of items. How does you solution work for [1, 2, 3, 4, 5, 6] as the input? It should also return the value [5, 3, 1]

Hint: get the odd index members, then reverse it.

Post back if you need more help. Good luck!

Andrei Oprescu
Andrei Oprescu
9,547 Points

Thank you!

This really helped. I will remember this way of thinking.