Problem in 13/15


#1

I really don't know whats wrong here:

def purify(seq):
    for i in seq:
        if i%2==1:
            while i in seq: 
                seq.remove(i)
    return seq

returns:

> Your function fails on purify([4, 1, 5, 1341, 34, 6, 8]). It returns [4, 5, 34, 6, 8] when it should return [4, 34, 6, 8].

#2

First of all, let's delete the line with the while, because it actually does nothing :slightly_smiling:

The problem you code has, is that the seq.remove will shorten your list, which you are working with in your for-loop. So when you remove 1, the for loop wants to use the next number in the list, but because it got shortened, it will go skip one number.

I will try to explain it to you:

The for loop uses some kind of counter, so for i in seq, will say i = 0 in the first run.

So what the for loop does, is to use seq[i], so i will count the rounds.

Usually it would go i = 1, i = 2 and so on.

But when reach the 5 in your list, i is 2 at the moment, so even if you remove the 5 from the list, the counter is on 2 still, so it will go on with 3 at the next round.

That makes seq[3], but in the round of 1, you remove the 1, so the list is shortened, but the counter is still the same.

Let me show you on an example:

list_1 = [4,6,7,9,13]

for i in list_1:
    if i%2 == 1:
        list_1.remove(i)

so the result would be, that it removes the 7 on round 2 (counting starts at 0), so next round will be 3.

list_1[3] = 13, because your current list is [4,6,9,13]

:smiley:

Instead of removing the numbers, add the non-prime numbers to a new list and return it.

For example:

def purify(seq):
    new_list = []
    for i in seq:
        if (i%2)!=1:
            new_list.append(i)
    return new_list

I hope you understood all of it and it helped you :smiley: