The list.remove()
function modifies the list in place and returns None
.
So when you do last.remove(i)
, it will remove the first occurrence of i
from the list last
and return None
, so lastone
will always be set to None
.
For what you are trying to do, you probably want all occurrences of an item from stop_words
removed so last.remove()
will not be the most efficient method. Instead, I would do something like the following with a list comprehension:
stop_words = set(["the", "of", "a", "to", "be", "from", "or"])
last = lower_words.split()
last = [word for word in last if word not in stop_words]
Converting stop_words
to a set is to make this more efficient, but you would get the same behavior if you left it as a list.
And for completeness, here is how you would need to do this with remove()
:
stop_words = ["the", "of", "a", "to", "be", "from", "or"]
last = lower_words.split()
for word in stop_words:
try:
while True:
last.remove(word)
except ValueError:
pass