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

Final part of the code challenge doesn't pass even though the right value is returned in pyCharm

list_of_numbers = [1, 2, 3, 4, 5]

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


doing: print(reverse_evens(list_of_numbers)) gives me [5, 3, 1] as the description asks for when i made this in pycharm but doesn't let me pass. Can anyone see where I'm going wrong?

the challenge is I need to create a function that will return each even index in reverse order in a list

slices.py
def first_4(full_list):
    first_4_items = full_list[:4]
    return first_4_items

def first_and_last_4(full_list):
    first_4_items = full_list[:4]
    last_4_items = full_list[-4:]
    all_items = first_4_items + last_4_items
    return all_items

def odds(full_list):
    odds_from_list = full_list[1::2]
    return odds_from_list

def reverse_evens(full_list):
    reverses_list = full_list[::-2]
    return reverses_list

1 Answer

Steven Parker
Steven Parker
229,920 Points

You're halfway there. Your method will work when the list has an odd number of items. But when the list has an even number of items, 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.

Excellent! Thanks! I got it :D I used modulo to work out if the len of the list was odd or even and took it from there :)