I keep getting an error saying "Your function fails on purify([4,5,5,4]). It returns [4,5,4] when it should return [4,4]."

I'm not sure why the other 5 is not being removed from the list. I found another solution using the append function to add only the even numbers to a new list, but is there a way to remove the odd numbers from the list instead?

Replace this line with your code. 

def purify(numbers):
    x = list(numbers)
    for i in x:
        if i % 2 != 0:
    return x


the problem is index shift, the moment you remove something from the list everything to the right of the removed item shifts one index to the left, while the loop progresses to the next index

knowing this, we can do two things:

not remove from the same list as we are looping over

we can loop over the list in reverse, this way we are not bothered by index shift


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.