Register Login

Difference between del, remove and pop Methods in Python

Updated Mar 25, 2020

In this article, we will learn about the differences between delete, remove and pop methods in python.

All these methods are built-in methods available in python. These methods are used for either deleting or removing items from a list.

1) Python remove() Function

The remove function takes an element as an argument and removes it from a defined list. If the element doesn’t exist in the list, python throws valueError exception.

Syntax:

List_name.remove(element)

Example: remove()

petlist = ['dog', 'cat', 'mouse', 'rabbit']
petlist.remove('mouse')
print ('Updated list of pets:', petlist)

Output

Updated list of pets: ['dog', 'cat', 'rabbit'])

Explanation

In the above example, we first defined a list called ‘petlist’. Then we used the remove() function to remove ‘mouse’ from the list. Then after removing the list element, we printed the updated list.  

2) Python pop() Function

Python pop() function is used to return the removed element from the given list. It takes the index value of an element as an argument. If there is no index value, python will throw index error: pop index out of range exception.

The index value is an optional parameter for pop() function. If no parameter is passed, the default index -1 is taken and it returns the last item from the list.

Syntax

List_name.pop(index_no)

For Example

#list of Languages
language = ['Hindi', 'English', 'Marathi', 'Bengali', 'urdu']
#Return value from pop()
#When 4 is passed
return_value = language.pop(4)
print('Return Value: ', return_value)
# Updated List
print('Updated List: ', language)

Output

Return Value:  urdu

Updated List:  ['Hindi', 'English', 'Marathi', 'Bengali']

Explanation

In the above example, we used the pop() function to pop an element at a specified index. In this case, the element we popped is at index 4. And then we printed the returned value i.e ‘urdu’. In the last line of the code we printed the updated list.

If we do not provide any index value then

language = ['urdu', 'English', 'Marathi', 'Bengali', 'hindi']
# Return value from pop()
# When no index is passed
return_value = language.pop()
print('Return Value: ', return_value)
# Updated List
print('Updated List: ', language)

Output 

Return Value:  hindi

Updated List:  ['urdu', 'English', 'Marathi', 'Bengali']

Explanation

In the above example, we did not passed an index value. And when no index value is given, then the last element is popped out. In this case, the last element is ‘Hindi’.

3) Del Python List Function

We use del() method to delete an item not to return any value. Using del() method we can also delete a range of value from the given list of elements.

Syntax

del List_name(index)

Example

numberslist = [1, 2, 3, 4, 5, 6]
# deleting the third item
del numberslist[2]
print ('the output list :', numberslist )

Output

the output list : [1, 2, 4, 5, 6]

Explanation

In the above code, we used the del method to remove the element at a specified index. In this case, we removed the element at index 2 i.e element ‘3’.

Deleting Items from 2nd to 5th

numberslist = [10, 21, 43, 54, 51, 36]
# deleting the third item/slice
del numberslist[2:5]
print ('the output list :', numberslist )

Output

the output list : [10, 21, 36]

Explanation

In the above example, we deleted the elements from index ‘2’ to ‘5’. The element at index 5 is not included but the element at index 2 is included.

Deleting All Elements

numberslist = [1, 2, 3, 4, 5, 6]
# deleting all elements
del numberslist[:]
print ('the out put list :', numberslist )

Explanation

In the above example, we deleted all the elements of the list ‘numberlist’. And we printed the list we got an empty list.

Note: only the items/elements of a list are deleted and not the list.

Output: 

the output list : []

Conclusion

Python pop() vs remove() vs del function 

  • The remove() function removes the first matching value from the list.
  • The pop() function is used to return the removed element from the list.
  • The del() function is used to delete an element at a specified index number in the list.


×