Remove a String from a list of Strings in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove a String from a list of Strings in Python #

Use the list.remove() method to remove a string from a list of strings, e.g. my_list.remove('apple'). The list.remove() method will remove the first string from the list whose value is equal to the supplied string.

main.py
my_list = ['apple', 'banana', 'kiwi', 'avocado'] # ✅ Remove string from list by value if 'kiwi' in my_list: my_list.remove('kiwi') print(my_list) # 👉️ ['apple', 'banana', 'avocado'] # ---------------------------------------------- # ✅ Remove string from list by index my_list.pop(0) print(my_list) # 👉️ ['banana', 'avocado'] # ---------------------------------------------- # ✅ Remove one or more strings from list (list comprehension) my_list = ['apple', 'application', 'banana', 'kiwi'] new_list = [item for item in my_list if not 'ap' in item] print(new_list) # 👉️ ['banana', 'kiwi'] # ---------------------------------------------- # ✅ Remove one or more strings from list (for loop) my_list = ['apple', 'application', 'banana', 'kiwi'] for item in my_list.copy(): if 'ap' not in item: my_list.remove(item) print(my_list) # 👉️ ['apple', 'application']

The first example uses the list.remove() method to remove a string from a list by value.

main.py
my_list = ['apple', 'banana', 'kiwi', 'avocado'] if 'kiwi' in my_list: my_list.remove('kiwi') print(my_list) # 👉️ ['apple', 'banana', 'avocado']

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

The method raises a ValueError if there is no such item.

The remove() method mutates the original list and returns None.

You can also use a try/except statement if you need to handle a scenario where the string is not in the list.

main.py
my_list = ['apple', 'banana', 'kiwi', 'avocado'] try: my_list.remove('TEST') except ValueError: pass

If the specified string is not present in the list, the except block runs.

Alternatively, you can use the pop() method.

Use the list.pop() method to remove a string from a list, e.g. my_list.pop(0). The list.pop() method will remove the string at the specified position and will return the removed value.

main.py
my_list = ['apple', 'banana', 'kiwi', 'avocado'] my_list.pop(0) print(my_list) # 👉️ ['banana', 'kiwi', 'avocado']

The list.pop method removes the item at the given position in the list and returns it.

If no index is specified, the pop() method removes and returns the last item in the list.

If you need to remove one or more strings from a list, use a list comprehension.

main.py
my_list = ['apple', 'application', 'banana', 'kiwi'] new_list = [item for item in my_list if not 'ap' in item] print(new_list) # 👉️ ['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 string ap is not present in the current string and return the result.

The new list doesn't contain any strings that contain the substring ap.

The list comprehension approach is suitable if you need to remove one or more strings from a list.

If you only need to remove a single string from the list, use the list.remove() or list.pop() methods.

An alternative approach is to use a simple for loop.

To remove a string from a list of strings:

  1. Use a for loop to iterate over a copy of the list.
  2. On each iteration, check if the current string meets a condition.
  3. Use the list.remove() method to remove the strings that meet the condition.
main.py
my_list = ['apple', 'application', 'banana', 'kiwi'] for item in my_list.copy(): if 'ap' in item: my_list.remove(item) print(my_list) # 👉️ ['banana', 'kiwi']

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

On each iteration, we check if the string ap is contained in the current string and use the list.remove() method to remove the matching elements.

If you only need to remove the first matching element, use the break statement to exit the for loop.

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

The break statement breaks out of the innermost enclosing for or while loop.

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

If you try to iterate over the original list and remove items from 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.