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

What is wrong with my code?

I have run this in my python shell and it works. def first_4(itb): return itb[:4]

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

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

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

r = reverse_evens([1, 2, 3, 4, 5]) print(r)

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

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

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

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

r = reverse_evens([1, 2, 3, 4, 5])
print(r)

1 Answer

Elad Ohana
Elad Ohana
24,456 Points

Hi Martin,

That only works when you have an odd number of items. For example, on the list [1, 2, 3, 4, 5] it does return [5, 3, 1], which are indexes 4, 2, 0. But, if you have the list [1, 2, 3, 4, 5, 6], it will return [6, 4, 2], which are indexes 5, 3, 1, which means it should return [5, 3, 1] instead. My solution was to first slice the even numbers, and then reverse it.

Hope this helps.