0

What's that difference between pop() and del keyword in python dictionaries?

https://code.sololearn.com/ca8HnWD0vTDj/?ref=app

13th Mar 2019, 11:47 AM
Ashutosh Dash
Ashutosh Dash - avatar
5 Respuestas
+ 6
pop doesn't only delete, it also returns, what it deletes. So you can write a statement like: x = your_list.pop()
13th Mar 2019, 11:58 AM
HonFu
HonFu - avatar
+ 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'))
14th Mar 2019, 9:41 AM
HonFu
HonFu - avatar
+ 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]
13th Mar 2019, 12:24 PM
rahim
rahim - avatar
+ 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
25th Mar 2019, 12:01 AM
Mostafa Tarek
Mostafa Tarek - avatar
0
HonFu rahim Can you refer from the code given in description?
14th Mar 2019, 9:32 AM
Ashutosh Dash
Ashutosh Dash - avatar