The effects of the three different methods to remove an element from a list:
remove removes the first matching value, not a specific index:
>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]
del removes the item at a specific index:
>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
and pop removes the item at a specific index and returns it.
>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
Their error modes are different too:
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range
Answer from Martijn Pieters on Stack OverflowThe effects of the three different methods to remove an element from a list:
remove removes the first matching value, not a specific index:
>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]
del removes the item at a specific index:
>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]
and pop removes the item at a specific index and returns it.
>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]
Their error modes are different too:
>>> a = [4, 5, 6]
>>> a.remove(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
>>> del a[7]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range
>>> a.pop(7)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: pop index out of range
Use del to remove an element by index, pop() to remove it by index if you need the returned value, and remove() to delete an element by value. The last requires searching the list, and raises ValueError if no such value occurs in the list.
When deleting index i from a list of n elements, the computational complexities of these methods are
del O(n - i)
pop O(n - i)
remove O(n)
del or remove to delete an item from a list
Difference between del and remove? - Post.Byes
What is the difference between __del__, __delete__, and __delitem__? Also, when should we use these? Purpose?
Difference between pop and remove
Videos
Hi, I'm currently learning Python using, among others, automatetheboringstuff.com
It's a great resource, and I enjoy hacking through the projects.
I am now finishing Chapter 8 (https://automatetheboringstuff.com/chapter8/), and I'm doing the first project, "Create the Quiz File and Shuffle the Question Order".
In that project, at one point, we need to remove an item from a list. Said item's value is stored in a variable. I decided to use the .remove command, like this:
my_list = ['cat', 'dog', 'duck', 'rabbit'] item_to_be_removed = 'dog' my_list.remove(item_to_be_removed)
But the author uses instead the "del" command, such as:
del my_list[my_list.index(item_to_be_removed)]
My question: is there a difference ? It seems much easier to use the first method. I understand that "del" will delete an item using it's index number, and "remove" will remove the first instance of the value we want to delete. But in the context of the project, I don't see why del was chosen.
(Both "del" and ".remove" were explained in previous chapters of the book)