Remove the first N elements from a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove the first N elements from a List in Python #

Use list slicing to remove the first N elements from a list, e.g. new_list = my_list[n:]. The new list won't contain the first N elements of the original list.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f'] # ✅ remove first N elements from list (using list slicing) n = 2 new_list = my_list[n:] print(new_list) # 👉️ ['c', 'd', 'e', 'f'] # ------------------------------------ # ✅ remove first N elements from list (using del with list slicing) del my_list[:n] print(my_list) # 👉️ ['c', 'd', 'e', 'f']

We used list slicing to remove the first N elements 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.

The slice my_list[n:] starts at the Nth item and goes to the end of the list.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f'] n = 2 new_list = my_list[n:] print(new_list) # 👉️ ['c', 'd', 'e', 'f']

The new list starts including elements at index 2 of the original list (the third list element).

If the value for the stop index is omitted, the slice goes to the end of the list.

Alternatively, you can use the del statement.

Use the del statement with list slicing to remove the first N elements from a list, e.g. del my_list[:n]. The del statement will remove the first N elements from the original list.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f'] n = 2 del my_list[:n] print(my_list) # 👉️ ['c', 'd', 'e', 'f']

We used the del statement with list slicing to remove the first N elements 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 the first N elements from a list.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f'] n = 4 del my_list[:n] print(my_list) # 👉️ ['e', 'f']
The list slice my_list[:n] starts at the beginning of the list and goes up to but not including index n.

The stop index is exclusive, so if we want to remove the first 4 items from a list, we specify a stop index of 4 to remove the elements at index 0, 1, 2 and 3.

You can also assign the first N list items and the items after to variables, on a single line.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f'] n = 3 before, after = my_list[:n], my_list[n:] print(before) # 👉️ ['a', 'b', 'c'] print(after) # 👉️ ['d', 'e', 'f']

The before variable stores the first 3 elements of the list and the after variable stores the elements after the first 3.

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.