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

SUVODEEP DUBEY
SUVODEEP DUBEY
2,470 Points

def reverse_evens(sl): return(sl[-1:-(len(sl)+1):-2]) Returning the reverse evens for task 4

def reverse_evens(sl): return(sl[-1:-(len(sl)+1):-2])

Please verify I am getting the correct output for reverse_evens

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

def first_and_last_4(sl):
    fir_4 = sl[:4]
    las_4 = sl[-4:]
    j_fir_las = []
    j_fir_las.extend([i for i in fir_4])
    j_fir_las.extend([i for i in las_4])
    return j_fir_las

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

def reverse_evens(sl):
    return(sl[-1:-(len(sl)+1):-2])

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The solution can not be found in a single slice since the number of items in the argument is unknown. Using your code:

>>> def reverse_evens(sl):
...     return(sl[-1:-(len(sl)+1):-2])
>>> reverse_evens([0,1,2,3,4,5])
[5, 3, 1]
>>> reverse_evens([0,1,2,3,4,5,6])
[6, 4, 2, 0]

Hint: First get the even-indexed items, then reverse them

Post back if you need more help. Good luck!!!