Remove list elements that contain given String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Remove list elements that contain given String in Python #

Use a list comprehension to remove the list elements that contain a given string, e.g. new_list = [item for item in my_list if substring not in item]. The list comprehension will return a new list without the elements that contain the given string.

main.py
my_list = ['banana', 'apple', 'application', 'test'] substring = 'ap' # ✅ remove elements from list that contain given string new_list = [item for item in my_list if substring not in item] print(new_list) # 👉️ ['banana', 'test'] # --------------------------------------------------- # ✅ remove elements from list that contain given string using for loop for item in my_list.copy(): if substring in item: my_list.remove(item) print(my_list) # 👉️ ['banana', 'test']

We used a list comprehension to remove the elements from a list that contain a certain string.

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 current list item doesn't contain the specified substring and return the result.

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

main.py
print('he' in 'hello') # 👉️ True print('abc' in 'hello') # 👉️ False

The new list consists of the strings that don't contain the specified substring.

If you need to remove the items from a list that contain a given substring, ignoring the case, convert both strings to lowercase.
main.py
my_list = ['banana', 'aPPLE', 'ApPLICATION', 'test'] substring = 'AP' # ✅ remove elements from list that contain given string new_list = [ item for item in my_list if substring.lower() not in item.lower() ] print(new_list) # 👉️ ['banana', 'test']

We used the str.lower() method to convert each string and the substring to lowercase.

The str.lower method returns a copy of the string with all the cased characters converted to lowercase.

The method doesn't change the original string, it returns a new string. Strings are immutable in Python.

To check if the substring is contained in the string in a case-insensitive manner, we have to convert both strings to lowercase or uppercase.

Note that the list comprehension doesn't change the contents of the original list, it returns a new list.

An alternative to using a list comprehension is to use a simple for loop.

To remove the elements from a list that contain a given string:

  1. Use a for loop to iterate over a copy of the list.
  2. Check if each string contains the specified substring.
  3. Use the list.remove() method to remove the strings that contain the substring.
main.py
my_list = ['banana', 'apple', 'application', 'test'] substring = 'ap' for item in my_list.copy(): if substring in item: my_list.remove(item) print(my_list) # 👉️ ['banana', 'test']

We used the list.copy() method to get a copy of the list.

main.py
my_list = ['banana', 'apple', 'application', 'test'] # 👇️ ['banana', 'apple', 'application', 'test'] print(my_list.copy())

The list.copy method returns a shallow copy of the object on which the method was called.

This is necessary because we aren't allowed to remove elements from a list while iterating over it.

However, we can iterate over a copy of the list and remove elements from the original list.

main.py
my_list = ['banana', 'apple', 'application', 'test'] substring = 'ap' for item in my_list.copy(): if substring in item: my_list.remove(item) print(my_list) # 👉️ ['banana', 'test']

On each iteration, we check if the current list item contains the specified substring and if it does, we use the list.remove() method to remove it.

The list.remove() method removes the first item from the list whose value is equal to the passed in argument.

If you need to remove the elements from the list that contain the given substring, ignoring the case, convert both strings to lowercase.

main.py
my_list = ['BANANA', 'APPLE', 'APPLICATION', 'TEST'] substring = 'ap' for item in my_list.copy(): if substring.lower() in item.lower(): my_list.remove(item) print(my_list) # 👉️ ['banana', 'test']

The most important thing to note when removing items from a list while iterating is to use the list.copy() method to iterate over a copy of the list.

If you try to remove elements from the original list while iterating over it, you might run into difficult to locate bugs.

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.