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

Adam Cameron
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Adam Cameron
Python Web Development Techdegree Graduate 16,731 Points

Hopelessly stuck on this challenge - slice of a list with all even indexes in reverse

After some googling I'm not getting any closer to the answer here and would like to ask for help. I can't figure out where to start the slice without knowing whether the iterable in question has an even or odd number of indexes.

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


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

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

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

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

If you want to use a one-line, remember you can take a slice of a slice of the form: item[::x][::y] (trying not to give actual answer). Post back if you need more help! Good Luck!!

james south
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
james south
Front End Web Development Techdegree Graduate 33,271 Points

you can, for instance, test for length and cut off the last element if it has an odd index, then slice every other element in reverse, or slice every other element from the beginning, then reverse. either will work. your function doesn't have to be a one-liner.

Adam Cameron
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Adam Cameron
Python Web Development Techdegree Graduate 16,731 Points

Alright, it's the next morning and I'm at hair-tearing out levels. Can someone please tell me what's wrong with this:

def reverse_evens(item):
    if len(list(item)) % 2 == 0:
        return item[::-2]
    else:
        return item[-1::-2]
Adam Cameron
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Adam Cameron
Python Web Development Techdegree Graduate 16,731 Points

UPDATE: I realize that starting at -1 for iterables with an odd number of indexes is incorrect since that's still the -first- index at the end of the iterable. However, it doesn't work when the slice starts at -2 either.

Adam Cameron
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Adam Cameron
Python Web Development Techdegree Graduate 16,731 Points

UPDATE 2: Okay, clearly there is something about the whole 'number of indexes' thing that was confusing to me because I had this backwards - this ended up working:

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

I wasn't counting "0" as an index and that meant that the lists I was making in the shell to test everything looked like they had an even number of indexes if they ended in an even number, but they were actually odd!

Alright, gonna go sit in the corner with the pointy cap on for a while.