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)
Pop vs Del
python - Which is faster? `list.pop(0)` vs `del list[0]`? - Stack Overflow
Difference between pop and remove
I was surprised at how slow list.pop() is! And list.remove() is even many times slower
Videos
Can someone explain me what the difference between pop and remove is that makes sense? Why would you use pop over remove or vice versa?
Thanks!
I know there is a list.clear(), I'm just sharing that I didn't expect that using list.pop() and list.remove() specifically could slow down the program that much.
li = list(range(500000))
Creating a list is quick.
So we are going to test out pop/remove specific values. For the purpose of this "benchmark", we are going to remove all elements from the list:
while (li):
li.pop(0)
It took 74.735 seconds to pop all the elements! It's ridiculously long.
I KNOW it would have been much faster if I even had used li.pop() without the index or maybe used filter function, list comprehension with conditional or whatever
But that's what I'm trying to show, how slow it is to remove certain list items specifically using pop and remove methods.
And li.remove(), which always requires a specified value to remove, is even worse than pop!
for num in li:
li.remove(num)This one took me 303.268 seconds to complete. How crazy it is.
I've been having fun with abstract data structures. Implemented linked lists and a queues running on linked lists.
And for the sake of interest, I decided to compare the performance of the queue based on the linked list and the usual python list. And I was surprised. When my linked list Queue dequeued 500.000 elements in 0.5 seconds, while python list Queue was doing it in 75 seconds.
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)