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

What is wrong with the final step. It works perfectly fine in the shell

What wrong with this this code?

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


def first_and_last_4(itr):
    return itr[0:4] + itr[-4:]


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


def reverse_evens(itr):
    del itr[1::2]
    return itr[::-1]

2 Answers

Steven Parker
Steven Parker
231,269 Points

I tried this for myself to see that it does appear to work. But I'm baffled as to why — since a slice returns a copy of a list, I don't understand how a "del" operation performed on it can possibly affect the original list.

But as a suggestion for passing the challenge: instead of deleting the items you don't want, try extracting the ones you do want and then reversing them.

I dont know how to do this, i tried using the pop feature but you cant say:

new_list = itr.pop([0::2])

Lol realised imidiately after posting that i can just do:

whatever = itr[0::2]

So i got it, ty!