Open In App

Remove words containing list characters - Python

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

In this article, we will explore various methods to remove words containing list characters in Python. The simplest way to do is by using a loop.

Using a Loop

Iterate over each word in the list and check if it contains any of the characters from remove_chars. If a word contains any of those characters then it will be excluded from the final result.

Python
a = ['gfg', 'is', 'best', 'for', 'geeks']

# List of characters to check for
remove_chars = ['g', 'e']

# Initialize an empty list to store result
res = []

# Loop through each word
for word in a:
  
    # Check if word contains any of characters from remove_chars
    if not any(char in word for char in remove_chars):
        res.append(word)

print(res)

Output
['is', 'for']

Explanation:

  • any(char in word for char in remove_chars): Checks if any character in remove_chars exists in the word.
  • res.append(word): Adds the word to the result only if it doesn't contain any of the characters from remove_chars.

Using List Comprehension

List comprehension is provides a compact and efficient way to achieve the same result in one line.

Python
a = ['gfg', 'is', 'best', 'for', 'geeks']

# List of characters to check for
remove_chars = ['g', 'e']

# Using list comprehension to filter out words containing any of characters
res = [word for word in a if not any(char in word for char in remove_chars)]

print(res)

Output
['is', 'for']

Explanation:

  • List comprehension: Iterates over the list 'a' and includes a word in the result only if it does not contain any of the characters from remove_chars.
  • any(): Ensures that words with the specified characters are excluded.



Explore