How To Remove A List Of Words From A String In Python

Remove a list of words from a string in Python

Python is a language used for many tasks, including string manipulation. One common string manipulation task is removing a list of words from a given string. Today, we’ll show you how to remove a list of words from a string in Python using some built-in functions such as split(), join(), and replace().

Remove a list of words from a string in Python

Here are some ways to approach this problem. Even a beginner can easily understand and practice them.

Using split(), join() functions, and list comprehension

We can use the split() and join() functions to split a string into a list of words and then join the list back together, excluding the words you want to remove. Additionally, list comprehension can create a new list from an existing one.

We’ll first split the original string into a list of words using split(). Then, we’ll use a list comprehension to create a new list where the values ​​that should be removed from the original string are deleted from this list too. Finally, join them into a string using the join() function. Like this:

unnecessaryWords = ['knowledge', 'and', 'about']
string = 'Learn and Share knowledge about IT'
print('Before:', string)

# Use split() to split a string into a list
listOfString = string.split()
print('List of words:', listOfString)

# Create a list of words with unnecessary words removed
necessaryWords = [word for word in listOfString if word.lower() not in unnecessaryWords]
print('List of necessary words:', necessaryWords)

# Join the list of necessary words into a string
result = ' '.join(necessaryWords)
print('After:', result)

Output:

Before: Learn and Share knowledge about IT
List of words: ['Learn', 'and', 'Share', 'knowledge', 'about', 'IT']
List of necessary words: ['Learn', 'Share', 'IT']
After: Learn Share IT

Using remove() function

The remove() function removes an element from a list.

Syntax:

list.remove(value)

Description:

This function removes the value from the list.

The remove() function is a great way to remove a list of words from a string. Simply loop through the list of words to be removed, and then call remove() on the list of words created by split() and pass in each word you want to remove as an argument to the remove() function. The function will take care of the rest. Finally, join them into a string using the join() function.

unnecessaryWords = ['knowledge', 'and', 'about']
string = 'Learn and Share knowledge about IT'
print('Before:', string)

# Use split() to split a string into a list
listOfString = string.split()
print('List of words:', listOfString)

# Remove all unnecessary words using remove()
for unnecessaryWord in unnecessaryWords:
    listOfString.remove(unnecessaryWord)

print('After using remove():', listOfString)

# Join the list of necessary words into a string
result = ' '.join(listOfString)
print('After:', result)

Output:

Before: Learn and Share knowledge about IT
List of words: ['Learn', 'and', 'Share', 'knowledge', 'about', 'IT']
After using remove(): ['Learn', 'Share', 'IT']
After: Learn Share IT

Using replace() function

We have covered the syntax of replace() in this article. You can read it if you want to understand it better.

There are several ways to remove a list of words from a string. One way is to use the replace() method. This method will replace all occurrences of the word in the string with another string.

This method is conceptually similar to the one described above. We’ll go through the list of words to be removed. In each loop, we will use replace() to replace the word to be deleted with an empty string. Continue in this manner until the loop is completed, at which point our string will no longer contain the values from the list of words to delete. Because replacing the word to be deleted will lead to a space, we will continue to use replace() to replace two spaces into one. It’s a little difficult to say; see the example below for a better understanding.

unnecessaryWords = ['knowledge', 'and', 'about']
string = 'Learn and Share knowledge about IT'
print('Before:', string)

# Remove all unnecessary words using replace() 2 times
for unnecessaryWord in unnecessaryWords:
    string = string.replace(unnecessaryWord, '').replace('  ', ' ')

print('After:', string)

Output:

Before: Learn and Share knowledge about IT
After: Learn Share IT

Summary

We’ve shown you three ways to remove a list of words from a string in Python in this article. We recommend using the remove() function when you reencounter the same task. We hope you found this article useful and learned something from it.

Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *