Problem with removing stopword

I have a problem with this part of the assignment.
I try two codes and both have problems:
1.
for word in stopwords:
token = " " + word + " "
sentence = sentence.replace(token, " “)
sentence = sentence.replace(” ", " ")
Error:
Expected Output:

'go store get snack'

My output:

'i go store get snack'

2.
modified_sentence = “”

START CODE HERE

for word in sentence.split(" "):
if word not in stopwords:
modified_sentence = modified_sentence + word + " "

sentence = modified_sentence

My output:

'go store get snack '

For the second response, I have an extra space at the end. Besides, I think it is better to solve it without using extra memory.
Thank you in advance for your help.

1 Like

Hello Iman!

Wonderful to have you join the Discourse!

I would look to a list comprehension that only added words that are not in stopwords, and then use the " ".join() method to turn them back into a sentence!

Please let me know how this approach works for you!

Thanks :smiley:

3 Likes

Hi Chris,

Thank you so much. That worked like a charm :star_struck:

Best,
Iman

1 Like