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

Chris Komaroff
PLUS
Chris Komaroff
Courses Plus Student 14,198 Points

Not passing challenge 4 of 4 - slices.py

Am I missing something? This is weird. I think I have the right answer. Easy python slice question, can someone help me? See my solution for reverse_evens(). Thanks!

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


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


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

# This correct: [5,3,1]
#def reverse_evens(iter):
#    return iter[-1::-2]

# This is correct: [5,3,1]
#def reverse_evens(iter):
#    return iter[4::-2]

# This is correct: [5,3,1]
def reverse_evens(iter):
    return iter[::-2]

5 Answers

Hi Chris,

The slices you have tried will only work part of the time. They would either work on odd length iterables or even length, but not both.

You could set up some if/else condtional logic to check the length of the iterable and then do the appropriate slice.

But you could accomplish this in 1 line by doing 2 slices. The first slice would get the even indexes and a slice on that to reverse it.

Chris Komaroff
Chris Komaroff
Courses Plus Student 14,198 Points

Last suggestion is really cool, just one line of code. Thank you.

Taylor Schimek
Taylor Schimek
19,318 Points

Hey Chris, iter[::-2] starts at -1. What if -1 isn't even?

Thomas Helms
Thomas Helms
16,816 Points

I see your logic, since I had the same concept as well. What threw me off was the hint of [5,3,1]. The problem is that those numbers aren't at even indexes; they are evenly spaced, sure.

Jason has the right direction: start by slicing the evens, in the normal way [::2]. Then iterate through that slice backward.

The list in the example was [1, 2, 3, 4, 5] The even indexes are 0, 2, and 4 and the numbers at those indexes are 1, 3 and 5

If you reverse the even indexes you would get [5, 3, 1]

Taylor Schimek
Taylor Schimek
19,318 Points

Need to check the length of the list that is coming in. It won't necessarily be the same as the example. Once you know the length, you can proceed accordingly, either list[::-1] or list[::-2]

It's easier to do 2 slices but if you're going to check the length then [::-1] isn't going to work. That's only going to reverse the list.

Taylor Schimek
Taylor Schimek
19,318 Points

Right. My bad. Meant start at -1 or -2. I did it by checking the length.