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

Reverse_evens: When I type this code into a test python document it returns the correct values. Help?

I don't know what I am doing wrong here. My first question is what is the exact items that it needs. Does it need all the even indexes or odds, because the example shows odds? Secondly, am I even close and I am just making a simple mistake or am I way off based?

slices.py
def first_4(word):
    return word[:4]
def first_and_last_4(word):
    return word[:4] + word[-4:]
def odds(word):
    return word[1::2]
def reverse_evens(word):
    return word[-2::-2]

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Nikolas,

The last part of this challenge is a bit tricky, but the key thing here is that they are looking for even indexes, not even numbers. So although the example shows a list of odd numbers, they were all at even indexes. Remember that the indexes start counting at zero.

The issue with starting at the end of the list as you are doing, is that you don't know how long the list is. If you pass in a list with an odd number of items, your last item will fall on an even index. And for a list with an even number of items, your last item will have an odd index. This will throw off your reverse step counter since you don't know if its starting at an odd or even index.

The easiest way to think about this one is to do it in two stages. First, get all the items at an even index, and second, reverse it.

Let me know if you need anymore help.