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

Paulhan Carre
Paulhan Carre
1,368 Points

What am i doing wrong?

need help with last part

slices.py
def first_4(a):
    return a[:4]
def first_and_last_4(fal):
    return first_4(fal) + fal[-4:]
def odds(o):
    return o[1::2]
value[2,4,6,8,10]
def reverse_evens(re):  
    return [::-1]
reverse_evens(value)
Jose Aguirre
Jose Aguirre
14,866 Points

It seems like the first three are correct but the last one needs a little extra. Right now you are just returning the list in reverse, remember you only want elements with an even index returned so set the list equal to [::2] so that it odd index is not added to list. This is what I got.

def reverse_evens(re):
  re = re[::2]
  re = re[::-1]
  return re

Hope this helps. BTW, nice job calling the first function in the second function, that did not occur to me, but it is quite elegant.

1 Answer

Steven Parker
Steven Parker
231,269 Points

You need to return only every other value.

This approach will only reverse the list, but you still need to return only the items that originally had even-numbered indexes.

You'll also either need to calculate the starting position based on the list size, or you'll need to extract the evens first and then reverse them in a separate step.