Remove every Nth element from a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 15, 2022

banner

Photo from Unsplash

Remove every Nth element from a List in Python #

Use list slicing to remove every Nth element from a list, e.g. del my_list[::2]. The value between the square brackets is the step which can be used to remove every Nth element from the list.

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

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

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

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

If you need to remove every Nth element from a list starting at the Nth element, e.g. remove every third element from a list, starting at the third element, specify the start index.

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

The start index is n - 1, so we start removing elements from the Nth element onwards.

If you want to remove every Nth 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] # ✅ remove every second element from list 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 Nth 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 in the example above, 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 the step is set to 3, the list slice selects every third list item.

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

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

When the start index is omitted, we start removing items from the list at index 0.

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

Here is an example that starts at index 2 and removes every second element from the list.

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

The list slice starts at index 2 and we remove every second item (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.