Practise Makes Perfect: 12: purify


Practise Makes Perfect: 12: purify

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

I feel like this is the classic "Your loop breaks as soon as it hits 'return' " problem? It looks from the error at least that it is removing one odd number, so it seems like the function works but isn't looping.
Only I don't know how to resolve this...I can't nest the return function any lower (or do i mean higher?) without it being outside of the function.

Thanks in advance for your help

def purify(list):
    for n in list:
        if n % 2 != 0:
    return list


Don't remove from original list,as it will lead to unexpected results.
(at each iteration when it sees an odd number and deletes the item from it,it will change the length of list and it will make your code to leave few items )

Create an empty list and add all even number there and return it ?


Thanks once again greentreemee


cheers!Keep up the good work! :thumbsup:


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