# Purify fails on repeated odd numbers

#1

I've written a code for purify (removing odd numbers from a list), but I can't work out what's wrong with it at all. It seems to fail when the list contains repeated odd numbers.

For example the error message I'm getting is Oops, try again. Your function fails on purify([4, 5, 5, 4]). It returns [4, 5, 4] when it should return [4, 4].

Also when I try purify([1,1,1,1,1,1]) it returns [1,1,1]

What's going on here? How do I fix it?

``````def purify(numbers):
for c in numbers:
if c%2 != 0:
numbers.remove(c)
return numbers``````

#2

And there is why. Give it a minute and think what is happening with that command.

There is a cure, but I'll wait till you get back to this topic before going any further.

#3

I'm still not sure why using remove() will only remove it one time--perhaps my understanding of the process of iteration is not correct?

Let's say there's a list = [1, 3, 3]
My understanding is that the command "for c in list" would first look at 1, and then at 3, and then at 3. However, is it the case that python lumps the two 3s together, meaning that "for c in list" only goes over the entry "3" once?

#4

Short answer, no. No lumping.

``[],[],[],[]``

Remove one.

``[],[],[]``

Which one we remove matters not. The remainder shift left to fill the void. This creates the problem of skipping. Give that some thought, and we may resume.

#5

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