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

ALEX PAYOD
seal-mask
.a{fill-rule:evenodd;}techdegree
ALEX PAYOD
Python Web Development Techdegree Student 1,270 Points

how to get the correct values for the reverse_evens()

what am i doing wrong here?

slices.py
def first_4(ittteeer):
    return(ittteeer[0:4])
def first_and_last_4(ittteeer2):
    return(ittteeer2[0:4]+ittteeer2[-4:])
def odds(ittteeer3):
    return(ittteeer3[1::2])
def reverse_evens(ittteeer4):
    return(ittteeer4[::-2])

2 Answers

Thomas McDonnell
Thomas McDonnell
8,212 Points

Hi Alex,

First well done getting as far as you did, that last one was tricky took me a while to see what was actually happening.

I always recommend opening up an interactive shell and trying out the code with different inputs, this will really help you see and understand what is actually happening behind the scenes.

So that being said if we look at the example below you will see that your code is actually passing the test case given however if we vary the input we do not get the desired result.

In[4]:list_ = [1,2,3,4,5]

In [5]: list_[-1::-2]
Out[5]: [5, 3, 1]

In [6]: l = [1,2,3,4]

In [7]: l[-1::-2]
Out[7]: [4, 2]   #here the out put is not as expected why?

this is down to the fact we are starting at the fourth index in the second example and so the out put is index's 3,1

def reverse_evens(iterrable):
       return iterrable[::2][::-1] .# first slice the evens, second slice to reverse 
ALEX PAYOD
seal-mask
.a{fill-rule:evenodd;}techdegree
ALEX PAYOD
Python Web Development Techdegree Student 1,270 Points

Thanks Thomas:)). Great help. This was in the back of my mind but the example in the challenge got me confused. I kept thinking of the even indexes starting from last, and not the actual even numbers. Great lesson also in putting the slices in one line.

Steven Parker
Steven Parker
231,248 Points

As Thomas explained, you're half right. Your method will work when the list has an odd number of items. But when the list has an even number of items, it will return reverse odds instead.

To always return reverse evens, there's two basic strategies:

  • compute the starting position based on the list size
  • extract the even index values first, then reverse them (as Thomas did)

Either one will pass the challenge when implemented correctly, in case you'd like to try it the other way.

Thomas McDonnell
Thomas McDonnell
8,212 Points

Hi Steven,

Thanks you explained it in a much clearer way than I.

How long have you been a treehouse member to have achieved so many point? I can only aspire ..... :)

Steven Parker
Steven Parker
231,248 Points

Thomas, I thought your explanation was good; but I wanted to point out that there was another method that could also be used. And I generally prefer to give hints rather than explicit solutions.

Looking at my profile to refresh my memory, I've apparently been a Treehouse student since May 30, 2014.