Delete an element from a dictionary

The del statement removes an element: Note that this mutates the existing dictionary, so the contents of the dictionary changes for anybody else who has a reference to the same instance. To return a new dictionary, make a copy of the dictionary: The dict() constructor makes a shallow copy. To make a deep copy, see the copy module. Note that making a copy for every … Read more

When is del useful in Python?

Firstly, you can del other things besides local variables Both of which should be clearly useful. Secondly, using del on a local variable makes the intent clearer. Compare: to I know in the case of del foo that the intent is to remove the variable from scope. It’s not clear that foo = None is doing that. If somebody just assigned foo … Read more