Purify help



"Oops, try again. Your function fails on purify([4, 5, 5, 4]). It returns [4, 5, 4] when it should return [4, 4]."

I expected that all odd numbers would've been removed.

def purify(lst):
    for i in lst:
        if i == 1:
        elif i % 2 != 0:
    return lst


Our primary goal is to remove all odd numbers from the list.
One thing to note that here youre trying to remove items from original list, This will make items of main list get shifted and their index will change,when every time you remove an odd number from it.

Make a copy of original list say mst

mst = list(lst)
loop over original list and if you find that a number is odd (while checking through conditional statement) remove that number from copy-list.
finally return the copy list as its purified list having only even numbers


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