Purify


#1



Help me to execute this! i unable to find the error in this


https://www.codecademy.com/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].


This is my code 
   def purify(n):
    for i in n:
        if i % 2 != 0:
            n.remove(i)
    return n


#2

remove causes index shifts, which mean a second successive odd number gets skipped

try the opposite approach: append constants to a new list


#3

which is the opposite approach here? didnt get you!


#4

append the even numbers to a new list


#5

Yes.. I did ..But again it showing errors.like below,

Oops, try again. Your function fails on purify([1]). It returns [1] when it should return [].

my code is here:
def purify(n):
list = []
for i in n:
if i % 2 == 0:
list.append(i)
return n


#6

your return n, which is the original list. You should return the list to which you appended the even numbers


#7

oh yes..thats my mistake..i didnt notice that.. Thank you


#8

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

This is your code ,lets see what your code does.
1. Your function purify takes a list.
2. It loops through each item of your list.
3. It checks if item is odd or not.
4. If it is odd then it removes item from list.
Now lets read what question is asking--

solution:
Define an empty list.
And as it says not to change original list, leave the old list.
What you do now?
You check for even numbers and append them in new list ?
The last part tho (The returning) ,I'm leaving upto you.
I hope it helps! :slight_smile:


#9

the problem is already solved?


#10

:grinning: When I finished my answer I saw It has been answered(and probably solved)! :thumbsup:


#11

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