I’m getting some output I don’t understand from the first part f the exercise.
The first thing I do is split the sentence into a list of words. That appears to work. I’ve printed out the list as words1. Then I loop through the list and check each work to see if the word is in the stopwords list. If it is I remove it from the list of words. I’ve printed out each word that gets removed. Some words on the list are not being identified as in the list. Oddly ‘to’ gets removed the first time but not the second time it is checked. It is also not identifying ‘am’ and ‘the’ as being in the stopwords list. I’ve also printed out the joined words as word2. Here is my output. Can you point me in the right direction?
words1 [‘i’, ‘am’, ‘about’, ‘to’, ‘go’, ‘to’, ‘the’, ‘store’, ‘and’, ‘get’, ‘any’, ‘snack’]
word i
word about
word to
word and
word any
words2 [‘am’, ‘go’, ‘to’, ‘the’, ‘store’, ‘get’, ‘snack’]
Out[15]:
‘am go to the store get snack’
Hi @George_Paslaski ,
It sounds like you’re facing some challenges with words not being removed as expected. This can happen due to the way lists are modified while iterating over them.
When you remove items from a list as you loop through it, the list’s length changes, but the loop counter doesn’t adjust for the removed items. This can cause the loop to skip over items. Imagine you’re in line for a movie, and suddenly a few people in front of you leave the line. You and everyone behind you move forward, but the ticket counter doesn’t skip anyone; it continues serving the next person in line.
A good strategy to deal with this is to create a new list where you only add what you want to keep. This way, you’re not changing the list you’re iterating over.
Here’s a hint: Try using a list comprehension to create a new list that includes only the words that are not in your stopwords list. This method involves going through each word in your sentence and adding it to a new list if it doesn’t match any stopword.
Remember, Python’s list comprehension is a powerful tool that can help you create new lists by applying an expression to each item in an iterable, allowing you to filter items based on a condition.
I encourage you to try this approach and see if it resolves the issue. If you have more questions or need further guidance, feel free to ask!
Let me know if this works,
Ado, 