To remove an individual item from a list in Python you can use either a list method or the
del statement, both approaches mutate the original list – meaning the list being operated on changes according to the instruction.
To remove multiple items from a list you can use the
del statement with the slice operator, or you can use a list comprehension with an if condition to filter the unnecessary items in your original list. Using the
del statement will mutate the original list, whereas using a list comprehension will create a new list leaving the original untouched.
To remove an individual element from a list in Python you can use the list method
pop as follows:
index_number is an optional number you can use to specify which element from the list you want to remove. If an
index_number is not provided the method will remove the last element from the list.
Here is a running example of using the
pop list method multiple times on the same list:
my_list = [1, 2, 3] # without inserting any index number into the parameter: my_list.pop() print(my_list) > [1, 2] # specifically removing the first item from the list by its index position: my_list.pop(0) print(my_list) >  # what happens when you use an invalid index number? my_list.pop(1) > Traceback (most recent call last): File "<input>", line 1, in <module> IndexError: pop index out of range
As you can see from the above examples, when the
.pop() list method does not contain anything for its parameter then the list will lose its final element.
If you insert a valid index number it will remove that specific element, according to its index number in the list, from the list.
Finally, if you do use an invalid index number in the
.pop() parameter you will receive an
IndexError that the
index is out of range.
Notice also that throughout the examples above the original list is modified according to the changes. This is called mutation and may not be the best outcome when trying to debug your code.
Another popular way of deleting individual items from a list is by using the
del statement on the specific item within the list you want to remove.
With the previous
.pop() list method if an index was not inserted into the parameter of the method the last item would be removed. The danger with using the
del statement is that it will remove the entire list if you’re not careful with your specific instruction!
Here is a running example when using the
del statement multiple times on the same list:
my_list = [1, 2, 3, 4, 5] # specifically deleting the first item from the list del my_list print(my_list) > [2, 3, 4, 5] # 1 benefit of using del over pop is deleting successive ranges within a list # here let's delete the last two items del my_list[-2:] print(my_list) > [2, 3] # when you don't declare a specific item: del my_list print(my_list) > Traceback (most recent call last): File "<input>", line 1, in <module> NameError: name 'e' is not defined
In the running examples above notice how using
del operates in the same way as
.pop() when specifying an index number – it simply removes that item from the list.
However, one benefit over using
.pop() is that the
del statement allows us to delete multiple items from a list depending on how the range is defined by the slice operator. In the example used above, the starting item to use in the range is the second last index denoted by the number
-2, followed by the range colon
: and then by a blank value before closing the range.
This slice operation means: select all the items in the range from the second last item to the end of the list.
You can similarly reverse some of these numbers to remove the first set of elements from a list too, as shown next.
nth Items From List
Coupling both the del statement and the handy slice operation you can perform some neat tricks on deleting multiple items according to their index position.
To delete the first n items in the list, simply use the slice operator as follows
[:n] where n is the number of items from the front of the list you want to remove.
Here is an example:
my_list = [1, 2, 3, 4, 5] # delete first 2 items del my_list[:2] print(my_list) > [3, 4, 5]
Delete Every Second (Or
nth) Item From List
To delete every nth item from the list with one command using the
del statement and the slice operator as follows:
[start:stop:nth]. For example, this would delete every second item from the list:
As the slice operator contains 3 variables, the first being which index to start, the second being which index to stop, and the third being the step, by setting the slice operation on the list to
[1::2] it will delete the second index item (index of 1) and then jump 2 spots and delete each until the end.
The pattern when deleting the nth item from a list would be as follows:
Here is a demonstration of deleting the second element in a list using this method:
my_list = [1, 2, 3, 4, 5, 6, 7] # delete every 2nd item del my_list[1::2] print(my_list) > [1, 3, 5, 7] my_list = [1, 2, 3, 4, 5, 6, 7] # delete every 3rd item del my_list[2::3] print(my_list) > [1, 2, 4, 5, 7]
Delete All Items From List Of Indexes
How would you delete items from a list if given a list of index numbers? For example, suppose you had a list of items
my_list defined as follows, and another list defined as
delete_indexes and you wanted to remove the items in the initial list according to the indexes in the
my_list = [1, 2, 3, 4, 5, 6, 7] delete_indexes = [1, 5, 6] # result to be => [1, 3, 4, 5]
Unfortunately, you cannot use the
.pop() list method as this only accepts 1 index number for its sole parameter. You also cannot use the
del statement combined with the slice operator unless there’s a linear step to the index numbers being removed.
So what can you use?
You could use a list comprehension. What’s best about using this handy one-liner is that it doesn’t mutate the original list!
Here is an example:
my_list = [1, 2, 3, 4, 5, 6, 7] delete_indexes = [1, 5, 6] new_list = [x for idx, x in enumerate(my_list) if idx not in delete_indexes] print(new_list) > [1, 3, 4, 5]
By using the one-liner for loop and wrapping it in a list and enumerating that list to expose the index number of the element in the list being looped, you can then append an if condition to filter if the index (
idx) is NOT in the
If the index is found in the list then the item is filtered out, but if it does satisfy the condition with the index number NOT being in the
delete_indexes list then the item is added to the new list.
This has achieved the benefit of being able to remove multiple items from a list, using a simple one-liner without requiring the need to import any additional libraries. Nice!
There are three ways to remove items from a list in Python. The pop list method and the del statement will both mutate the original list, whereas using a list comprehension creates a new list.
Each approach has its additional benefits, so choose the one most appropriate to your needs.