One line for loop to add elements to a list in Python

Question:

I am trying to remove stop words (from nltk) from my data set but not sure why the one line query is not working:

filtered_words = [word if word not in stop_words for word in words]

This is what I need to do:

filtered_words = []
for word in words:
    if word not in stop_words:
        filtered_words.append(word)
Asked By: Aditya Landge

||

Answers:

the if has to be at the end of the list comprehension:

filtered_words = [word for word in words if word not in stop_words]

see: https://www.pythonforbeginners.com/basics/list-comprehensions-in-python

Answered By: AlessioM

syntax you want is :

x = [x for x in range(200) if x%3 == 0 ]

put condition behind for

the syntax you have requires else like :

x = [x if x%3 == 0  else None for x in range(200)  ]

and this produces an error:

x = [x if x%3 == 0  for x in range(200)  ]
Answered By: user8426627

The syntax is backwards.
[appending_word for word in starting_words if word not in stop_words]


starting_words = ["hi", "joshing", "afflate", "damage"]
stop_words = ["afflate", "K", "books"]
filtered_words = []
'''for word in starting_words:
    if word not in stop_words:
        filtered_words.append(word)
==

filtered_words = [word for word in starting_words if word not in stop_words]'''
Answered By: LifeLifeScienceLife
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.