1

So, I am writing a code that will cross check every word in my list with a list of common words, if the word in my Alist is = to the common word it will be removed. I am trying to keep it basics to terms I learnt to not confuse my studying.

Alist = ["the", "baby", "is", "so", "happy"]
common = ["the", "so"]
for x in Alist:
    for i in common:
        if i == Alist[x]:
            Alist.remove(common[i])

1 Answers1

0

You can use list-comprehension to filter the words:

Alist = ["the", "baby", "is", "so", "happy"]

# better convert the common words to set (for faster searching):
common = {"the", "so"}

Alist = [word for word in Alist if word not in common]
print(Alist)

Prints:

['baby', 'is', 'happy']
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91