Python Introducing Lists Meet Lists Deletion

Bruce McMinn
Bruce McMinn
9,800 Points

another del vs .pop question

What's the difference between del and .pop?

I was trying to see what it is, and maybe this is a bad example of how del and .pop are different, but the output is the same. This looks to me like I end up with the same values in each list. Maybe items in each list is the right way to say it...

OK, so let me check this as well, shapes and my_shape are both objects, their type is list, each has an id that stays constant, and their values are mutable? So is "item" something else entirely?

shapes = ["square", "circle", "polygon"]
print(shapes)
my_shape = shapes[0]
print(my_shape)
del shapes[0]
print(shapes)
print(my_shape)

del shapes

shapes = ["square", "circle", "polygon"]
print(shapes)
my_shape = shapes.pop(0)
print(my_shape)
print(shapes)
print(my_shape)

Thanks for your help.

1 Answer

Steven Parker
Steven Parker
182,590 Points

The difference between "del" and "pop" is the kind of argument you provide to it (the item vs. just an index), and that "pop" returns the value that is being removed.