Remove first or last N elements from a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
8 min

banner

# Table of Contents

  1. Remove the First N elements from a List in Python
  2. Remove the Last N elements from a List in Python
  3. Remove every Nth element from a List in Python

# Remove the first N elements from a List in Python

Use list slicing to remove the first N elements from a list, e.g. 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 the first N elements from a list (using list slicing) n = 2 new_list = my_list[n:] print(new_list) # ๐Ÿ‘‰๏ธ ['c', 'd', 'e', 'f']

remove first n elements from list

The code for this article is available on GitHub

If you need to remove the last N elements from a list, click on the following subheading.

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.

# Remove the last N elements from a List in Python

Use list slicing to remove the last N elements from a list, e.g. 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 list using

The code for this article is available on GitHub

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.

Note: If you need to remove every Nth element from a List, click on the following subheading:

# Remove the first N elements from a List using del

You can also use the del statement to remove the first N elements.

main.py
my_list = ['a', 'b', 'c', 'd', 'e', 'f'] n = 2 del my_list[:n] print(my_list) # ๐Ÿ‘‰๏ธ ['c', 'd', 'e', 'f']
The code for this article is available on GitHub

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.

# Remove the last N elements from a List using del

The del statement can also be used 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']

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']
The code for this article is available on GitHub

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']

# 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 a 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 a list (starting at 0) del my_list[::2] print(my_list) # ๐Ÿ‘‰๏ธ [2, 4, 6, 8]

remove every nth element from list

The code for this article is available on GitHub

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

# Removing every second element from a list

The following code sample removes every second element from a list.

main.py
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] # โœ… Remove every second element from a list (starting at 0) del my_list[::2] print(my_list) # ๐Ÿ‘‰๏ธ [2, 4, 6, 8] # ------------------------------------------- # โœ… Remove every second element from a list starting at a 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]

removing every second element from list

The code for this article is available on GitHub

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.

# Remove every Nth element from a list starting at the Nth element

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.

Here is an example of removing every second element from a list starting at the second element.

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 Nth element from a list starting at index 0

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 a list del my_list[::2] print(my_list) # ๐Ÿ‘‰๏ธ [2, 4, 6, 8]
The code for this article is available on GitHub

The syntax for list slicing is my_list[start:stop:step]. The value of 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).

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev