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

Philip Schultz
Philip Schultz
11,437 Points

Stuck on task 4 -slice - reverse order - even index only

Hey all, Can not seem to find my mistake for the last task 'reverse_evens'. I would appreciate some help. Instructions below

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!

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

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Philip,

Imagine the input list [1, 2, 3, 4]. What would you expect as your output? What do you actually get as output?

Cheers

Alex

Philip Schultz
Philip Schultz
11,437 Points

Got it, thanks Alex

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