Nienke Luirink
Nienke Luirink

Reputation: 141

Removing certain words from sentences in a list

If I have a list that contains certain sentences and words, like:

res = ['Today is a great day', 'lunch @myplace', 'make sure to check this link: https://']

and I want to delete only the words that start with '@' or words that contain 'https', and not the whole sentence that contains that certain word, how do I go about doing that? Right now, I have the following:

words_filtered = [e.lower() for e in res]  
words_cleaned = [word for word in words_filtered if 'http' not in word and not word.startswith('@')]  

when printing words_cleaned, the words have indeed been deleted from the list but so has the whole sentence. It returns ['today is a great day'] But I want it to return ['today is a great day', 'lunch', 'make sure to check this link:']

Upvotes: 2

Views: 491

Answers (1)

Jan
Jan

Reputation: 43169

Praise the power of comprehensions here:

res = ['Today is a great day', 'lunch @myplace', 'make sure to check this link: https://']

words_cleaned = [" ".join([
                    words for words in sentence.split()
                    if 'https:' not in words and not words.startswith('@')])
                    for sentence in res]

print(words_cleaned)

This yields

['Today is a great day', 'lunch', 'make sure to check this link:']


Or, as @jpp points out, use

words_cleaned = [" ".join([
                    words for words in sentence.split()
                    if not ('https' in words or words.startswith('@'))])
                    for sentence in res]

Upvotes: 3

Related Questions