Purify 12/15


#1

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!


#2

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!


#3

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!


#4

@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:


Anti-Vowel
#5

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


#6

not work for met,this is my code below:

def purify(num):
     list_2=[4,5,4]
    for num in list_2:
        if num%2==0:
            continue
        else:
            list_2.remove(num)
    return list_2

#7

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


#8

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:
                    lst.remove(curr)
    return lst
lst=[1,1,1,1,1,2,3,4,5,6,7,8,9]
print purify(lst)

#9

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.


#10

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:
            new_lst.append(i)
        else:
            continue
    return new_lst
    
print purify([1,2,3,4,5,6])

#11

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