How to delete a slice from a list in Python

avatar

Borislav Hadzhiev

Last updated: Aug 19, 2022

banner

Photo from Unsplash

Delete a slice from a list in Python #

Use the del statement to delete a slice 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 = [1, 2, 3, 4, 5, 6, 7] # ✅ delete slice from list start_index = my_list.index(2) print(start_index) # 👉️ 1 stop_index = my_list.index(6) print(stop_index) # 👉️ 5 del my_list[start_index:stop_index] print(my_list) # 👉️ [1, 6, 7] # ----------------------------------- # ✅ remove multiple non-consecutive indexes from list my_list = [1, 2, 3, 4, 5, 6, 7] indexes = [1, 3, 5] result = [item for idx, item in enumerate(my_list) if idx not in indexes] print(result) # 👉️ [1, 3, 5, 7]

The first example uses the del statement to delete a slice from a list.

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 = [1, 2, 3, 4, 5, 6, 7] # ✅ delete slice from list start_index = my_list.index(2) print(start_index) # 👉️ 1 stop_index = my_list.index(6) print(stop_index) # 👉️ 5 del my_list[start_index:stop_index] print(my_list) # 👉️ [1, 6, 7]

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

main.py
my_list = [1, 2, 3, 4, 5, 6, 7] del my_list[:2] print(my_list) # 👉️ [3, 4, 5, 6, 7]
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 = [1, 2, 3, 4, 5, 6, 7] del my_list[2:] print(my_list) # 👉️ [1, 2]

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 = [1, 2, 3, 4, 5, 6, 7] del my_list[2:-1] print(my_list) # 👉️ [1, 2, 7]

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 = [1, 2, 3, 4, 5, 6, 7] step = 2 del my_list[2:7:step] print(my_list) # 👉️ [1, 2, 4, 6]

The example deletes every second list item starting at index 2 and going up to, but not including index 7.

If you need to remove multiple, non-consecutive indexes from a list, use a list comprehension.

main.py
my_list = [1, 2, 3, 4, 5, 6, 7] indexes = [1, 3, 5] result = [item for idx, item in enumerate(my_list) if idx not in indexes] print(result) # 👉️ [1, 3, 5, 7]

We used a list comprehension to iterate over the list.

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 index is not one of the indexes to be removed and return the result.

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 = ['apple', 'banana', 'melon'] for index, item in enumerate(my_list): print(index, item) # 👉️ 0 apple, 1 banana, 2 melon
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.