-2

I attempted to simplify this code:

for word in wordlist:
    if (word != 'a' and
        word != 'i' and
        word != 'and' and
        word != 'this' and
        word != 'is' and
        word != 'in'):

into

commom_words = ['a', 'i', 'and', 'this', 'is', 'in]

for word in wordlist:
   if word != any(common_words):

I also tried all(common_words).

Using the original code, the common words were omitted from my search of the text. When I tried to simplify them in the variable, the common words were getting through if the statement.

Laurel
  • 5,771
  • 12
  • 29
  • 54
Yianni S
  • 1
  • 1

1 Answers1

0

The easiest approach would probably be to use the not in syntax:

common_words = ['a', 'i', 'and', 'this', 'is', 'in']
for word in wordlist:
    if word not in common_words:
        # Do something with it...
Mureinik
  • 277,661
  • 50
  • 283
  • 320