Remove the last N elements from a List in Python

avatar

Borislav Hadzhiev

Last updated: Aug 16, 2022

banner

Photo from Unsplash

Remove the last N elements from a List in Python #

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

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

We used list slicing to remove the last 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[:len(my_list) - n] starts at the beginning of the list and goes up to, but not including the Nth element in the list.

The len() function returns the length (the number of items) of an object.

For example, a list with a length of 6 minus 2 (N) = 4. This means that we would select the list items at index 0, 1, 2 and 3.

If the value for the start index is omitted, the slice starts at index 0 of the list.

Alternatively, you can use the del statement.

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

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

We used the del statement with list slicing to remove the last 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[2] print(my_list) # 👉️ ['a', 'b'] del my_list[1] print(my_list) # 👉️ ['a', 'b']

We can use the del statement with a list slice to remove the last N elements from a list.

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

The list slice my_list[len(my_list) - n:] starts at Nth element and goes to the end of the list.

For example, a list with a length of 6 minus 2 (N) = 4. This means that we would select the list items at index 4 and 5.

You can also use negative list slices to remove the last N elements from a list.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f'] n = 2 del my_list[-n:] print(my_list) # 👉️ ['a', 'b', 'c', 'd']
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.

The list slice my_list[-2:] starts at the second-to-last item and goes to the end of the list.

However, note that this approach is only suitable if N is greater than 0.

For example, if you use this approach and set N to 0, you would remove all elements from the list.

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

Whereas if you use the first approach that uses the len() function, you wouldn't remove any items from the list.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f'] n = 0 del my_list[len(my_list) - n:] print(my_list) # 👉️ ['a', 'b', 'c', 'd', 'e', 'f']
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.