Eswar Ambati717 Points
Difference between del and pop
In the video at 3:54 I had a misunderstanding f what del really was.
This is because it was mentioned that del can be used when you don twant the value around and u want it to be garbage collected. However the value is still around due to such variable having the ability to store the value. As usin del only removes the label and not the actual value. Unlike pop
This might be my misunderstanding hopefully it can be cleared
Jason WiramPro Student 38,630 Points
The previous answer is not entirely correct. del removes an item at a specific index
> yourArray = [3, 2, 2, 1] > del yourArray [3, 2, 1]
pop removes an item at a specific index (and returns it)
> yourArray = [4, 3, 5] > yourArray.pop(1) 3 > yourArray [4, 5]
If pop is a method to the object list, e.g., books.pop(), what is the classification of del? Is it also a method? If so, how come it is done or written in a different way than all the methods we have learnt thus far?
Aaron Hinesley2,992 Points
Here is what I used to help me know the difference. If you use delete, you have to write more code to keep the value: example1 = "Hello"
example2 = example1
So now the string is saved with a new label. But there must be an easier way to do this right? That's when pop becomes the better way to do this.
example1 = ["Hello"]
example2 = books.pop()
Now the string is removed from the list and already has a new label with less script! I hope this helps in any way.