Purify help please



Error Message:

When I run my code in the terminal it work?
[4, 4]

def purify(x):
    for _ in x:
        if _ % 2 != 0:
    return x


an index shift occurs which causes the second successive odd number to skipped.

ways to overcome the problem: don't iterate over the same list as you are removing from.

personally, i would recommend the opposite approach : append even numbers to new list


Thank you. I'll reword the code to append. Greatly appreciated, stetim94!

I added an empty string with append.

def purify(x):
    even = []
    for _ in x:
        if _ % 2 == 0:
    return even


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