12. Purify problem with code


#1

what is wrong with my code ?


#2

You've defined a blank list (numlist = [ ]) and then you try to remove something from this blank list.
Why not try to change the condition to equal to, rather than not equal to, and add something to the list instead?


#3
  • It's not != but rather ==

  • .append() do not .remove() because whenever you use the remove() method, the relevant index will shift and the script will skip it.

Either than that your code is perfect.

def purify(lst):  #lst is short for list, remember we cannot use list due to its function in python  
    even_numbers = []
    for item in lst:
        if item % 2 == 0:
            even_numbers.append(item)
    return even_numbers

This should put you in prospective.
This is more readable due to the naming of the function and list desired.