Last updated: Apr 9, 2024
Reading timeยท8 min
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.
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']
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).
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.
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).
stop
index is omitted, the slice goes to the end of the list.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.
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']
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).
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
.
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:
del
You can also use the del
statement to remove the first N elements.
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.
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.
my_list = ['a', 'b', 'c', 'd', 'e', 'f'] n = 4 del my_list[:n] print(my_list) # ๐๏ธ ['e', 'f']
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.
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.
del
The del
statement can also be used to remove the last N elements from a list.
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.
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.
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.
my_list = ['a', 'b', 'c', 'd', 'e', 'f'] n = 2 del my_list[-n:] print(my_list) # ๐๏ธ ['a', 'b', 'c', 'd']
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.
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.
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']
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.
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]
We used the del statement with list slicing to remove every Nth element from a list.
The following code sample removes every second element from a list.
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]
The del
statement can be used to remove one or more list elements by index.
my_list = [1, 2, 3] del my_list[0] print(my_list) # ๐๏ธ [2, 3] del my_list[1] print(my_list) # ๐๏ธ [2]
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.
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.
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]
0
If you want to remove every Nth element from a list starting at index 0
, omit
the start
index.
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 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.
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.
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.
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.
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).
You can learn more about the related topics by checking out the following tutorials: