Can't purify


#1

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/2/2

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

how come it doesn't remove the second 5?

def purify(numbers):
    new_list = numbers
    for i in new_list:
        if i % 2 != 0:
           new_list.remove(i)
    return new_list


#2

Owing that a list is not a value, but a reference object we cannot copy by direct assignment. To copy a list to an independent variable use the List.copy() method; or you can use a list slice, n = numbers[:].

Iterate over the orginal list, numbers, afterward, and remove.from the copy in the loop. That should work.


#3

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