Why del is not considered good for deleting an object, rather pop should be used?
Removing an object from a list
Hey @Mohit2000, let me explain you this in detail :
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]
I hope this clears your doubt ! So there is nothing such as good or bad for deleting an object. These all are alternatives to do the same task.
Please mark the doubt as resolved in your doubts section !
Happy Learning !
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.