## Difference between del, remove and pop on lists

### Question

``````>>> 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 any difference between the above three methods to remove an element from a list?

2012/07/17
1
984
7/17/2012 10:27:57 AM

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
``````
2020/06/28
1408
6/28/2020 9:03:36 AM

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 latter 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)
``````
2012/07/17

Since no-one else has mentioned it, note that `del` (unlike `pop`) allows the removal of a range of indexes because of list slicing:

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

This also allows avoidance of an `IndexError` if the index is not in the list:

``````>>> lst = [3, 2, 2, 1]
>>> del lst[10:]
>>> lst
[3, 2, 2, 1]
``````
2017/02/27

Already answered quite well by others. This one from my end :)

Evidently, `pop` is the only one which returns the value, and `remove` is the only one which searches the object, while `del` limits itself to a simple deletion.

2020/06/20

Many best explanations are here but I will try my best to simplify more.

Among all these methods, reverse & pop are postfix while delete is prefix.

remove(): It used to remove first occurrence of element

`remove(i)` => first occurrence of i value

``````>>> 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(): It used to remove element if:

unspecified

`pop()` => from end of list

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

specified

`pop(index)` => of index

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

delete(): Its a prefix method.

Keep an eye on two different syntax for same method: [] and (). It possesses power to:

1.Delete index

`del a[index]` => used to delete index and its associated value just like pop.

``````>>>del a[1]
>>>a
[0, 1, 4, 6, 5]
``````

2.Delete values in range [index 1:index N]

`del a[0:3]` => multiple values in range

``````>>>del a[0:3]
>>>a
[6, 5]
``````

3.Last but not list, to delete whole list in one shot

`del (a)` => as said above.

``````>>>del (a)
>>>a
``````

Hope this clarifies the confusion if any.

2020/06/20

pop - Takes Index and returns Value

remove - Takes value, removes first occurrence, and returns nothing

delete - Takes index, removes value at that index, and returns nothing

2018/04/10