Remove multiple items from a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove multiple items from a List in Python #

To remove multiple items from a list:

  1. Use a list comprehension to iterate over the list.
  2. Check if each item is not one of the items to be removed.
  3. The new list won't contain any of the elements that were to be removed.
main.py
my_list = ['one', 'two', 'three', 'four', 'five'] elements_to_remove = ['two', 'three'] # ✅ Remove multiple elements from list (list comprehension) new_list = [item for item in my_list if item not in elements_to_remove] print(new_list) # 👉️ ['one', 'four', 'five'] # ------------------------------------------------ # ✅ Remove multiple elements from list (using for loop) my_list = ['one', 'two', 'three', 'four', 'five'] elements_to_remove = ['two', 'three'] for item in my_list.copy(): if item in elements_to_remove: my_list.remove(item) print(my_list) # 👉️ ['one', 'four', 'five'] # ------------------------------------------------ # ✅ Remove multiple elements from list by index (using enumerate) my_list = ['one', 'two', 'three', 'four', 'five'] indexes_to_remove = [1, 2, 3] new_list = [item for idx, item in enumerate( my_list) if idx not in indexes_to_remove] print(new_list) # 👉️ ['one', 'five']

The first example uses a list comprehension to remove multiple items from a list.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
main.py
my_list = ['one', 'two', 'three', 'four', 'five'] elements_to_remove = ['two', 'three'] new_list = [item for item in my_list if item not in elements_to_remove] print(new_list) # 👉️ ['one', 'four', 'five']

On each iteration, we check if the current item is not one of the items to be removed and return the result.

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

x not in l returns the negation of x in l.
main.py
print(3 not in [1, 2]) # 👉️ True print(3 not in [1, 2, 3]) # 👉️ False

The list comprehension doesn't mutate the original list, it returns a new list.

If you want to remove the items from the original list, use a for loop.

Remove multiple items from a List using a for loop #

To remove multiple items from a list:

  1. Use a for loop to iterate over a copy of the list.
  2. On each iteration, check if the current item is one of the items to be removed.
  3. Use the list.remove() method to remove the matching elements.
main.py
my_list = ['one', 'two', 'three', 'four', 'five'] elements_to_remove = ['two', 'three'] for item in my_list.copy(): if item in elements_to_remove: my_list.remove(item) print(my_list) # 👉️ ['one', 'four', 'five']

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 items from a list while iterating over it.

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

main.py
my_list = ['one', 'two', 'three', 'four', 'five'] elements_to_remove = ['two', 'three'] for item in my_list.copy(): if item in elements_to_remove: my_list.remove(item) print(my_list) # 👉️ ['one', 'four', 'five']
On each iteration, we check if the current item is one of the items to be removed and use the list.remove() method to remove the matching elements.

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

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

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.

If you need to remove multiple items from a list by index, use the enumerate() function.

Remove multiple items from a List using enumerate() #

To remove multiple items from a list:

  1. Use a list comprehension to iterate over the list.
  2. Use the enumerate function to get access to the index of the current iteration.
  3. Remove the matching items based on the index.
main.py
my_list = ['one', 'two', 'three', 'four', 'five'] indexes_to_remove = [1, 2, 3] new_list = [item for idx, item in enumerate( my_list) if idx not in indexes_to_remove] print(new_list) # 👉️ ['one', 'five']

The example removes multiple elements by index from a list.

The enumerate function takes an iterable and returns an enumerate object containing tuples where the first element is the index, and the second is the item.

main.py
my_list = ['one', 'two', 'three'] for index, item in enumerate(my_list): print(item, index) # 👉️ one 0, two 1, three 2

On each iteration, we use the not in operator to check if the current index is not one of the indexes we want to remove and return the result.

The new list doesn't contain any of the specified indexes.

Remove multiple items from a List using list slicing #

Use the del statement to remove multiple items from a list, e.g. del my_list[start_index:stop_index]. The del statement can be used to remove one or more list elements by index.

main.py
my_list = ['one', 'two', 'three', 'four', 'five'] start_index = my_list.index('two') print(start_index) # 👉️ 1 stop_index = my_list.index('four') print(stop_index) # 👉️ 3 del my_list[start_index:stop_index] print(my_list) # 👉️ ['one', 'four', 'five']

The syntax for list slicing is my_list[start:stop:step].

The start index is inclusive and the stop index is exclusive (up to, but not including).

Python indexes are zero-based, so the first item in a list has an index of 0, and the last item has an index of -1 or len(my_list) - 1.

We specified the start and end indexes when deleting a slice from the list in the example.

main.py
my_list = ['one', 'two', 'three', 'four', 'five'] del my_list[1:3] print(my_list) # 👉️ ['one', 'four', 'five']

If you omit the start index, the slice starts at the beginning of the list.

main.py
my_list = ['one', 'two', 'three', 'four', 'five'] del my_list[:3] print(my_list) # 👉️ ['four', 'five']
Note that the stop index is exclusive. The slice goes up to, but not including the stop index.

If you omit the stop index, the slice goes to the end of the list.

main.py
my_list = ['one', 'two', 'three', 'four', 'five'] del my_list[3:] print(my_list) # 👉️ ['one', 'two', 'three']

Negative indices can be used to count backwards, e.g. my_list[-1] returns the last item in the list and my_list[-2] returns the second-to-last item.

main.py
my_list = ['one', 'two', 'three', 'four', 'five'] del my_list[2:-1] print(my_list) # 👉️ ['one', 'two', 'five']

The stop index is exclusive, so the list slice goes up to, but not including the last item in the list.

You can also specify a step when deleting a slice from a list.

main.py
my_list = ['one', 'two', 'three', 'four', 'five'] del my_list[::2] print(my_list) # 👉️ ['two', 'four']

The example deletes every second element starting at index 0.

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.