3

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])

2 Answers 2

7

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']
Sign up to request clarification or add additional context in comments.

2 Comments

Technically, l is ['YES'].
Is there another I can accomplish this without having to save to l?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.