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 tested this question in workspaces and it still didn't give a right answer. Can anyone help?

Hi!

I have recently come across this question on a 'slices' challenge and I dont know how to do it. The question is 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!

The code that I used for this challenge is at the bottom of this question.

Can you tell me what I did wrong and how I can repair it?

thanks!

Andrei

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

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

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

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

1 Answer

Steven Parker
Steven Parker
231,269 Points

You're half right. Based on whether the list has an even or odd number of items, your method might return reverse evens. But otherwise, it will return reverse odds instead.

To always return reverse evens, there's two basic strategies:

  • compute the starting position based on the list size
  • extract the even index values first, then reverse them

Either one will pass the challenge when implemented correctly.