0
What's that difference between pop() and del keyword in python dictionaries?
5 Respostas
+ 6
pop doesn't only delete, it also returns, what it deletes.
So you can write a statement like:
x = your_list.pop()
+ 3
The logic is the same.
If you use del on a dictionary item, it is just deleted and that was it.
But if you use pop, it is literally like you take out the item of the dict and hold it in your hand, so you can directly store it in a variable or print it.
Try for example:
print(dict.pop('year'))
+ 2
del removes the item at a specific index
>>> sample_list =[5, 7, 8, 3]
>>>del sample_list[0]
>>>sample_list
[7, 8, 3]
pop() removes the item and returns it
>>> sample_list.pop(2)
3
>>>sample_list
[7, 8]
+ 1
pop() Removes the element with the specified key
The del keyword removes the item with the specified key name
The del keyword can also delete the dictionary completely