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

Claes Bell
Claes Bell
2,275 Points

What appears to be the correct answer to the 4th task isn't working

I have written a function which returns the correct output in my IDE but not in the workspace: def reverse_evens(list_4): return(list_4[::-2]) Am I missing something?

slices.py
def first_4(list_1):
    return(list_1[:4])
def first_and_last_4(list_2):
    return(list_2[:4]+list_2[-4:])
def odds(list_3):
    return(list_3[1::2])
def reverse_evens(list_4):
    return(list_4[::-2])

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Claes,

That code will work if the list contains an odd number of elements, but will not work if the list contains an even number of elements. (Try adding one more element to your list in your IDE and see what happens)
HINT: You will need to add a conditional to the method to check the length of the list being reversed.

Here is a post with a detailed out example of why it's incorrect. This post also provides the correct code if you scroll all the way down to the bottom, but give the task a try first with the HINT in mind, and then if you're still stuck, scroll that post for the code.

Keep Coding! :) :dizzy: