Remove every second element from a list in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Remove every second element from a list in Python #

Use the del statement with list slicing to remove every second element from a list, e.g. del my_list[::2]. The del statement will remove every second element from the list starting at the specified start index.

main.py
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # ✅ remove every second element from a list # starting at the second element n = 2 del my_list[n - 1::n] print(my_list) # 👉️ [1, 3, 5, 7, 9] # ------------------------------------------- # ✅ remove every second element from a list # starting at index 0 my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] del my_list[::2] print(my_list) # 👉️ [2, 4, 6, 8] # -------------------------------------------- my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # ✅ remove every second element from a list # starting at index 2 del my_list[2::2] print(my_list) # 👉️ [1, 2, 4, 6, 8]

We used the del statement with list slicing to remove every second element from a list.

The del statement can be used to remove one or more list elements by index.

main.py
my_list = ['a', 'b', 'c'] del my_list[0] print(my_list) # 👉️ ['b', 'c'] del my_list[1] print(my_list) # 👉️ ['b']
We can use the del statement with a list slice to remove every Nth element from a list.

If you need to remove every second element from a list starting at the second element, specify the start index.

main.py
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # ✅ remove every second element from a list # starting at the second element n = 2 del my_list[n - 1::n] print(my_list) # 👉️ [1, 3, 5, 7, 9]
The start index is n - 1, so we start removing elements from the Nth element onwards.

If you need to remove every second element from a list starting at index 0, omit the start index.

main.py
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] del my_list[::2] print(my_list) # 👉️ [2, 4, 6, 8]

The syntax for list slicing is my_list[start:stop:step]. The value for the start index is inclusive, whereas the value for the stop index is exclusive.

To remove every second element from a list, we only specified a step value.

When the start and stop indexes are omitted, the slice starts at the beginning (index 0) of the list and goes to the end.

The step value is set to 2, so we remove the list items at index 0, 2, 4, 6, etc.

In other words, we remove every second element from the list, starting at index 0.

If you need to remove every second element from a list starting at a specific index, provide a value for the start index.

main.py
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # ✅ remove every second element from a list # starting at index 2 del my_list[2::2] print(my_list) # 👉️ [1, 2, 4, 6, 8]

The list slice starts at index 2 and we remove every second element (2, 4, 6, etc).

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.