+ 3

What is difference between pop and del?

pop and del metods are same or not in python.

27th Jul 2018, 1:32 PM
Nitin Gutte
Nitin Gutte - avatar
1 ответ
+ 6
Apart from the slight difference in syntax, they do work differently. del() removes a list element from memory. pop(), however, returns the item that you are removing, allowing you to use it again if you wish. Eg: a = [1,2,3] b = [4,5,6] Say you wanted to take out the first item of list 'b' and add it to the end of list 'a'. Using pop, this can be done simply: a.append(b.pop(0)) b.pop(0) removes the first list item in b, and returns it so that a.append can use it. There is no simple way to do this (that I know of) using del. So: del(b[0]) removes 4 from b, but to add it to a, it would need to be done manually, i.e. a.append(4). Hope this makes sense.
27th Jul 2018, 2:02 PM
Russ
Russ - avatar