Purify help


#1



https://www.codecademy.com/en/courses/python-intermediate-en-rCQKw/2/2?curriculum_id=4f89dab3d788890003000096#


"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:
            lst.remove(i)
        elif i % 2 != 0:
            lst.remove(i) 
    return lst


#2

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


#3

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