Purify 12/15


I have been having problems with my code and I don't know what has gone wrong. Can someone please help me fix this?

Thank you!


The way you use .remove function will not remove any duplicate values, you will have to come up with a different approach. You could use a while loop like done here, or the other solutions mentioned, but it would be better to come up with your own solution!


Thanks for the tip. I decided to just .append any number that was even to a new list and return the new list rather than the list with the odd numbers removed, thank you once again for the help!


@stetim94 is correct, but I think he left the reason unexplained, which I will do:

I will illustrate the problem to you:

First iteration:

list_2 = [4,  5,  5,  4]
          ^- num

number is not odd, so it is not removed.
num moves to second number

Second iteration:

list_2 = [4,  5,  5,  4]
              ^- num

number is odd, so it is removed.
list_2 becomes [4, 5, 4]
num moves to third number

Third iteration:

list_2 = [4,  5,  4]
                  ^- num

Question: Can you see that 5 got skipped? That's the problem in your code.

And so your current approach won't work and you'll have to use append +new list which I see you are already doing so it's good :smile:


That explains a lot, i seems i have still something to learn :smile: thank you gaurangtandon


not work for met,this is my code below:

def purify(num):
    for num in list_2:
        if num%2==0:
    return list_2


The lesson will try a few different lists with different values, so you can't just make a fixed list, that is not how it works


i have the same issue. i tried to solve it by writing a for loop to go through and remove all matching values, however the first double matching value is still getting through although all others seem to be getting caught. here's my code i hope someone can point me in the right direction. many thanks

ignore just seen gaurangtandon's post and changed method but not sure why if the index skips this only left me with a single 1 from the test data. i think first iteration left me with 3 1's as skipped every other one then second iteration deleted first and third one leaving 1 in index position [0] while it moved onto index[2]

def purify(lst):
    for curr in lst:
        if curr%2==1:
            for x in lst:
                if x == curr:
    return lst
print purify(lst)


the counter keeps increasing, that is how your loop works. So, the first time it is lst[0] then lst[1], lst[2]. So, each time the index increases by one. But if items are removed, the index of items changes, but the loop doesn't "know" that, it will just keeping increasing the counter.


I tried to make it simple. Hope you guys will like it.

def purify(lst):
    new_lst = []
    for i in lst:
        if i % 2 == 0:
    return new_lst
print purify([1,2,3,4,5,6])


It nice and good, one question, why do you have an else? You can just leave it out