Remove a list of words from a string in Python

avatar

Borislav Hadzhiev

Last updated: Jul 8, 2022

banner

Photo from Unsplash

Remove a list of words from a string in Python #

To remove a list of words from a string:

  1. Split the string on each whitespace character.
  2. Use a list comprehension to filter out the list of words.
  3. Use the str.join() method to join the filtered list into a string.
main.py
words_to_remove = ['one', 'two', 'three'] my_str = 'apple one banana two kiwi three' list_of_words = my_str.split() print(list_of_words) # 👉️ ['apple', 'one', 'banana', 'two', 'kiwi', 'three'] filtered_words = [word for word in list_of_words if word.lower() not in words_to_remove] print(filtered_words) # 👉️ ['apple', 'banana', 'kiwi'] final_string = ' '.join(filtered_words) print(final_string) # 👉️ 'apple banana kiwi'

The first step is to use the str.split() method to split the string into a list of words.

main.py
my_str = 'apple one banana two kiwi three' list_of_words = my_str.split() print(list_of_words) # 👉️ ['apple', 'one', 'banana', 'two', 'kiwi', 'three']

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)
When the str.split() method is called without a separator, it considers consecutive whitespace characters as a single separator.

If the words in your string are separated by a different separator, make sure to pass it in the call to str.split(), e.g. str.split(',').

The next step is to use a list comprehension to filter out some of the words.

main.py
words_to_remove = ['one', 'two', 'three'] my_str = 'apple one banana two kiwi three' list_of_words = my_str.split() print(list_of_words) # 👉️ ['apple', 'one', 'banana', 'two', 'kiwi', 'three'] filtered_words = [word for word in list_of_words if word.lower() not in words_to_remove] print(filtered_words) # 👉️ ['apple', 'banana', 'kiwi']
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

On each iteration, we check if the word is not in the words_to_remove list before returning it.

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l, otherwise it evaluates to False.

x not in l returns the negation of x in l.

We also used the str.lower() method to convert each word to lowercase, which is optional.

Once we have a list that only contains the words we need, we can use the str.join() method to join the list of words into a string.

main.py
words_to_remove = ['one', 'two', 'three'] my_str = 'apple one banana two kiwi three' list_of_words = my_str.split() print(list_of_words) # 👉️ ['apple', 'one', 'banana', 'two', 'kiwi', 'three'] filtered_words = [word for word in list_of_words if word.lower() not in words_to_remove] print(filtered_words) # 👉️ ['apple', 'banana', 'kiwi'] final_string = ' '.join(filtered_words) print(final_string) # 👉️ 'apple banana kiwi'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We joined the strings in the list with a space separator in the example, but you can use any other separator that suits your use case.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.