views:

72

answers:

2

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])
+8  A: 

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']
bobince
Technically, `l` is `['YES']`.
Mike Graham
Good point... added version that preserves case.
bobince
Is there another I can accomplish this without having to save to l?
Protean
+1  A: 

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.

Mike Graham