Purify not working


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

I get the error message: "Oops, try again. Your function fails on purify([4, 5, 5, 4]). It returns [4, 5, 4] when it should return [4, 4]. "

The iteration skips an element, even thought I'm not modifying the list I'm iterating over. I don't know why.

def purify(numbers):
    result = numbers
    for number in numbers:
        if number % 2 == 1:
    return result


Oh, but you are. result is a reference to numbers. Try to make a slice copy...

result = numbers[:]


Oh, so if I change result, I actually also change the reference (in this case numbers)?
And by doing:

result = numbers[:]

I'm making 'result' independent?


Not also, only change it. result was not a list, per se.

That is correct. This will clone the list elements, and not keep a reference. Now we are changing the result list, and the numbers list does not see these changes.


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