Purify


#1



I don't understand whats wrong in my code

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


def purify(number):
    for x in number:
        y = x
        x = x % 2
        if x != 0:
            del number[number.index(y)]
    return number
n = [1, 2, 3, 4, 5, 6, 7, 8, 9]
n2 = [4, 5, 5]
print purify(n2)


#2

i wrote an explanation about it here:

yes, its for using .remove(), but using del gives the same index shift


#4

aaaah i get it now. i can use append to make a new list and return the new list. thanks for that help


#5

well, its not impossible, but pretty tricky

I recommend append even numbers to new list


#6

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