Remove list elements starting with given String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove list elements starting with given String in Python #

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

main.py
my_list = ['apple', 'application', 'dev', 'test'] substring = 'ap' # ✅ remove elements from list that start with given string new_list = [item for item in my_list if not item.startswith(substring)] print(new_list) # 👉️ ['dev', 'test'] # ----------------------------------------- # ✅ remove elements from list that start with given string (for loop) for item in my_list.copy(): if substring in item: my_list.remove(item) print(my_list) # 👉️ ['dev', '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 string doesn't start with the given 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.

x not in s returns the negation of x in s.
main.py
print('abc' not in 'hello') # 👉️ True print('he' not in 'hello') # 👉️ False

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

If you need to remove the items from a list that start with a given substring, ignoring the case, convert both strings to lowercase.
main.py
my_list = ['APPLE', 'APPLICATION', 'DEV', 'TEST'] substring = 'ap' # ✅ remove elements from list that start with given string new_list = [ item for item in my_list if not item.lower().startswith(substring.lower()) ] print(new_list) # 👉️ ['dev', '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.

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

To remove the elements from a list that start with a given string:

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

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.

On each iteration, we check if the current list item starts with 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.

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.