Purify


#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].


Since it removed the 5, it should remove the other 5.

please help. Thanks if you did!

def purify(x):
    for y in x:
        if y % 2 == 1:
            x.remove(y)
    return x


#2

the problem is that lists can't have empty indexes, so the moment you remove 5 from index 1, everything to the right shifts to the left, so the 5 of index 2 shifts to index 1, while the loop continues to index 2

so successive odd numbers are not removed

the easiest solution is the opposite approach, append even numbers to new list


#3

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