Remove multiple indexes from a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 20, 2022

banner

Photo from Unsplash

Remove multiple indexes from a List in Python #

To remove multiple indexes 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. Check if each index is not one of the ones to be removed and return the result.
main.py
my_list = ['one', 'two', 'three', 'four', 'five', 'six'] indexes_to_remove = (0, 1, 3) # ✅ Remove multiple indexes from a list at the same time (list comprehension) new_list = [item for idx, item in enumerate( my_list) if idx not in indexes_to_remove] print(new_list) # 👉️ ['three', 'five', 'six'] # ------------------------------------------------------- # ✅ Remove multiple indexes from a list (for loop) my_list = ['one', 'two', 'three', 'four', 'five', 'six'] indexes_to_remove = (0, 1, 3) for index, item in enumerate(my_list.copy()): if index in indexes_to_remove: my_list.remove(item) print(my_list) # 👉️ ['three', 'five', 'six'] # ------------------------------------------------------- # ✅ Remove multiple indexes from a list (list slicing) my_list = ['one', 'two', 'three', 'four', 'five', 'six'] del my_list[0:3] print(my_list) # 👉️ ['four', 'five', 'six']

The first example uses a list comprehension to remove multiple indexes 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', 'six'] indexes_to_remove = (0, 1, 3) new_list = [item for idx, item in enumerate( my_list) if idx not in indexes_to_remove] print(new_list) # 👉️ ['three', 'five', 'six']

On each iteration, we check if the current index is not one of the indexes 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('three' not in ['one', 'two']) # 👉️ True print('three' not in ['one', 'two', 'three']) # 👉️ False

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

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 indexes from a List using a for loop #

To remove multiple indexes from a list:

  1. Use a for loop to iterate over an enumerate object.
  2. Check if each index is one of the indexes to be removed.
  3. Use the list.remove() method to remove the matching indexes.
main.py
my_list = ['one', 'two', 'three', 'four', 'five', 'six'] indexes_to_remove = (0, 1, 3) for index, item in enumerate(my_list.copy()): if index in indexes_to_remove: my_list.remove(item) print(my_list) # 👉️ ['three', 'five', 'six']

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', 'six'] indexes_to_remove = (0, 1, 3) for index, item in enumerate(my_list.copy()): if index in indexes_to_remove: my_list.remove(item) print(my_list) # 👉️ ['three', 'five', 'six']
On each iteration, we check if the current index is one of the indexes 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, consecutive indexes from a list, use list slicing.

Remove multiple indexes from a List using list slicing #

Use the del statement to remove multiple indexes 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 from a list by index.

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

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', 'six'] del my_list[2:5] print(my_list) # 👉️ ['one', 'two', 'six']

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

main.py
my_list = ['one', 'two', 'three', 'four', 'five', 'six'] del my_list[:2] print(my_list) # 👉️ ['three', 'four', 'five', 'six']
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', 'six'] del my_list[2:] print(my_list) # 👉️ ['one', 'two']

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', 'six'] del my_list[2:-1] print(my_list) # 👉️ ['one', 'two', 'six']

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', 'six'] del my_list[1::2] print(my_list) # 👉️ ['one', 'three', 'five']

The example deletes every second element starting at index 1.

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.