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

Getting correct numbers in workspaces... but it says it isn't in the task?

So I used...

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

... for the fourth task in the challenge on slices and skipping steps. The question asked us to return the even indexes in a list but reversed. When I type this into workspace and enter the example given I get the correct numbers back but not here. Is there something I'm missing?

slices.py
def first_4(list):
    first_four = list[0:4]
    return first_four

def first_and_last_4(list):
    changed_list = list[0:4]
    changed_list += list[-4::1]
    return changed_list

def odds(list):
    changed_list = list[1::2]
    return changed_list

def reverse_evens(list):
    changed_list = list[::-2]
    return changed_list

1 Answer

Steven Parker
Steven Parker
229,644 Points

What this code does depends on whether the list has an even or odd number of items in it. In one case, it will indeed return the even indexed items in reverse. But in the other case it will return the odd indexed items in reverse.

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.

Thanks!