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

Martin Mõtsar
Martin Mõtsar
2,844 Points

#I can't get the right outputs from the last task, even though testing in PyCharm I get the sam results as in the sample

I tried two approaches to the task, but still it doesn't work. Am I way off the the task?

slices.py
numbrid=list(range(10))
def first_4(numbrid):
    x=numbrid[:4]
    return x
def first_and_last_4(numbrid):
    x1=numbrid[:4]
    x2=numbrid[-4:]
    return x1+x2
def odds(numbrid):
    x3=numbrid[1::2]
    return x3
def reverse_evens(numbrid):
    x3=numbrid[-1::-2]
    return x3

#or another approach
def reverse_evens(numbrid):
    x4=numbrid[::-1]
    x5=x3[::2]
    return x5

#I can't get the right outputs from the last task, even though testing in PyCharm I get the sam results as in the sample.

1 Answer

Steven Parker
Steven Parker
229,695 Points

If you start at the end, whether you get "evens" or "odds" depends on the size of the list. So it's easy to get a false validation based on your test data.

To always get reverse events, there's two basic strategies: you can compute the reverse starting position based on the length of the list, or you can extract all the even indexed items first and then reverse them with a separate slice (and they must be done in that order).