Tutorial :Python String Method Conundrum


The following code is supposed to print MyWords after removing SpamWords[0]. However; instead of returning "yes" it instead returns "None". Why is it returning "None"?

MyWords = "Spam yes"  SpamWords = ["SPAM"]  SpamCheckRange = 0  print ((MyWords.upper()).split()).remove(SpamWords[SpamCheckRange])  


Because remove is a method that changes the mutable list object it's called on, and returns None.

l= MyWords.upper().split()  l.remove(SpamWords[SpamCheckRange])  # l is ['YES']  

Perhaps you want:

>>> [word for word in MyWords.split() if word.upper() not in SpamWords]  ['yes']  


remove is a method of list (str.split returns a list), not str. It mutates the original list (removing what you pass) and returns None, not a modified list.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »