Is the wrong "remove"


#1



def purify(text):
----for i in range(len(text)):
------if text[i]%2 == 1:
---------text = text.remove(text[i])
----return text

in 15. Practice Makes Perfect --Purify


Replace this line with your code.


#2

There is very important thing to understand that some method changes an object on the fly but does not return anything (when a function does not return anything It returns None)

.remove() method changes list object on fly but it returns None so if you save it somewhere you're simply saving None in some variable.

so instead of this..

text = text.remove(text[i])

do this..

text.remove(text[i])

even you do this you still will get an error,reason is simple, you're looping over a fixed range but removing items of list (it changes length/index of list/list items)

so instead of removing from text list ,make a copy of it and remove from it.

one more thing text name is bit deceitful as it seems to be text but its a list, use something more comprehensible name like list1 etc


#3

Thank you , I got it.


#4

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