. Advertisement .
..3..
. Advertisement .
..4..
A list is among the very first few things that you will familiarize yourself with on your Python journey. However, it will accompany you throughout your remaining coding day.
That is the reason why we consider it important to know how to remove a single entry in a list with Python. After all, this knowledge will serve as the base for more advanced manipulation techniques that you will learn later.
Remove A Single Entry In A List With Python Using remove()
There is no use denying that remove() is among the most popular methods utilized in getting rid of a list’s elements. Its main job is to remove the first item with a matching value to the passed argument. That is why it has quite a simple syntax:
list.remove(element)
As you can see, there is only one input variable, the element. Whatever you pass in, remove() will search the list to find a matching value and delete the item. If there is no match, there will be an exception specified as:” ValueError: list.remove(x): x not in list”.
Otherwise, this function does not have any return value.
Here is an example:
string = ['a', 'b’, 'c', ‘d']
string.remove(‘c’)
print(string)
Output:
['a', 'b’, ‘d']
Remove A Single Entry In A List With Python Using pop()
Another just as popular method to get rid of a list’s elements is pop()
. Unlike the deletion that remove()
performs, this function takes an item out of the list and then returns it.
There is also a huge difference in how we should specify which item to remove. While remove()
demands the item’s specification, pop()
needs its index to be passed in as the argument.
For this reason, the syntax for pop()
is just as simple as remove()
, but they still have some unique differences.
list.pop(index)
While it also takes in one input, it doesn’t return an error message if nothing is passed in. Instead, pop() will default to the last item’s index, which is -1. If you pass in a value out of the list’s index range, it does pass an error message.
Example:
string = ['a', 'b’, 'c', ‘d']
string.pop(2)
print(string)
Output:
['a', 'b’, ‘d']
Remove A Single Entry In A List With Python Using The del Operator
As you can immediately observe, this time, we don’t use a function but an operator. This change allows for easier comprehension of if you are new to Python.
Otherwise, the del operator operates almost the same as pop()
, albeit it does not return the removed item. In other words, it takes in the item’s index as an argument, then permanently deletes that item from the list.
It also supports the removal of many items at once from the list. As a result, it makes it much easier to manipulate the list.
Example:
string = ['a', 'b’, 'c', ‘d']
del string[2]
print(string)
Output:
['a', 'b’, ‘d']
These approaches serve as the base for more advanced techniques like removing substring from a string.
Conclusion
We hope that after reading through this article, it is now fully clear to you how to remove a single entry in a list with Python. Each of the three methods that we have provided has some interesting strengths. As long as you remember this and know when to apply which, you will no longer struggle with this problem anymore.
Leave a comment