Is the wrong "remove"


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.


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


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


Thank you , I got it.


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