Aditya Landge
Aditya Landge

Reputation: 1245

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

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)

Upvotes: 4

Views: 19122

Answers (3)

LifeLifeScienceLife
LifeLifeScienceLife

Reputation: 1

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]'''

Upvotes: 0

user8426627
user8426627

Reputation: 943

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)  ]

Upvotes: 3

AlessioM
AlessioM

Reputation: 176

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

Upvotes: 9

Related Questions