Coder Perfect

On lists, what is the difference between del, remove, and pop?

Problem

>>> a=[1,2,3]
>>> a.remove(2)
>>> a
[1, 3]
>>> a=[1,2,3]
>>> del a[1]
>>> a
[1, 3]
>>> a= [1,2,3]
>>> a.pop(1)
2
>>> a
[1, 3]
>>> 

Is there a difference between the three techniques for removing a list element mentioned above?

Asked by sachin irukula

Solution #1

The following are the results of the three techniques for removing an element from a list:

The first matched value is removed, not a specific index:

>>> a = [0, 2, 3, 2]
>>> a.remove(2)
>>> a
[0, 3, 2]

del is a command that removes an item from a specific index:

>>> a = [9, 8, 7, 6]
>>> del a[1]
>>> a
[9, 7, 6]

and pop removes and returns the object at a particular index.

>>> a = [4, 3, 5]
>>> a.pop(1)
3
>>> a
[4, 5]

Their error modes are also distinct:

>>> 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

Answered by Martijn Pieters

Solution #2

To delete an element by value, use del to delete it by index, pop() to delete it by index if you need the returned value, and remove() to delete it by value. The latter needs searching the list, and if no such value is found, it raises a ValueError.

The computational complexity of these methods are increased when eliminating index I from a list of n entries.

del     O(n - i)
pop     O(n - i)
remove  O(n)

Answered by Sven Marnach

Solution #3

Because no one else has noted it, del (unlike pop) allows you to remove a range of indexes due to list.

>>> lst = [3, 2, 2, 1]
>>> del lst[1:]
>>> lst
[3]

This also prevents an IndexError from being thrown if the index isn’t in the list:

>>> lst = [3, 2, 2, 1]
>>> del lst[10:]
>>> lst
[3, 2, 2, 1]

Answered by Chris_Rands

Solution #4

Others have already provided excellent responses. This is from my perspective:)

Clearly, pop is the only one that returns the value, remove is the only one that searches the object, and del is limited to simple deletion.

Answered by Saurav Sahu

Solution #5

There are a lot of nice explanations here, but I’ll do my best to simplify things even further.

Remove and pop are both postfix techniques, whereas delete is prefix.

remove(): Removes the element’s initial occurrence. remove(n) => removes the first instance of n in the list.

>>> a = [0, 2, 3, 2, 1, 4, 6, 5, 7]
>>> a.remove(2)   # where i = 2
>>> a
[0, 3, 2, 1, 4, 6, 5, 7]

pop() is a function that removes an element…

>>> a.pop()
>>> a
[0, 3, 2, 1, 4, 6, 5]
>>> a.pop(2)
>>> a
[0, 3, 1, 4, 6, 5]

It’s a prefix method, del().

Keep a look out for two distinct syntaxes for the same method: [] and [] It has the ability to:

>>> del a[1]
>>> a
[0, 1, 4, 6, 5]
>>> del a[0:3]
>>> a
[6, 5]
>>> del (a)
>>> a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'a' is not defined

I hope this clears things up.

Answered by Mayur Patil

Post is based on https://stackoverflow.com/questions/11520492/difference-between-del-remove-and-pop-on-lists