It shows, "Oops, try again. Your function fails on purify([4, 5, 5, 4]). It returns [4, 5, 4] when it should return [4, 4]." when I don't find any mistake. Please help me get it


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


Next


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


#2

try create a new list instead of modifying the parameter.

new = []
for number in numbers:
if number % 2 == 0 :
new.append(number)


#3

Ok. The idea given by you is good one.
But what's mistake in my code, i wanna know.
Please find it.


#4

Well, the instructions told us not to directly modify the list.
Logically speaking, the mistake is that when we remove(number) the number just goneā€¦and replaced by the next number.

so your purify([4,5,5,4]) means when the if hit the first 5, it remove that number. Because the first 5 was removed, the current number is the second 5 and then the list just go on ignoring the current number.

so, logically the 5 in It returns[4,5,4] is the second 5


#5

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