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

appears to be a bug

When I run my code in REPL it works just not on the site.

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

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

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

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

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

1 Answer

While your formula gives the right answer for an odd named list, it does not for an even numbered list.

The prompt asks for a function that returns only even indexed iterables, so list[0,2,4,6,8...] your formula returns the right answer for odd length lists but not even ones.

def first_4(alist):
    return alist[:4]

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

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

def reverse_evens(lista):
    if len(lista) % 2 == 0:
        return lista[:-1][::-2]
    else:
        return lista[::-2]

The revised version tests for even / odd length adjusts the list accordingly to return only the even indexes.

lmk if this helps :)

Perfect thanks. Treehouse should have hinted or led with the even case.