Is there a way to delete an item from a dictionary in Python?
Additionally, how can I delete an item from a dictionary to return a copy (i.e., not modifying the original)?
Is there a way to delete an item from a dictionary in Python?
Additionally, how can I delete an item from a dictionary to return a copy (i.e., not modifying the original)?
Here a top level design approach:
I'm passing the dictionary and the key I want into my function, validates if it's a dictionary and if the key is okay, and if both exist, removes the value from the dictionary and prints out the left-overs.
Output:
{'B': 55, 'A': 34}
Hope that helps!
Result: d = {1: 2, '2': 3}
nice one-liner to check if the key is present, delete it, return the value, or default:
Below code snippet will help you definitely, I have added comments in each line which will help you in understanding the code.
or you can also use dict.pop()
or the better approach is
A
dict
is the wrong data structure to use for this.Sure, copying the dict and popping from the copy works, and so does building a new dict with a comprehension, but all that copying takes time—you've replaced a constant-time operation with a linear-time one. And all those copies alive at once take space—linear space per copy.
Other data structures, like hash array mapped tries, are designed for exactly this kind of use case: adding or removing an element returns a copy in logarithmic time, sharing most of its storage with the original.1
Of course there are some downsides. Performance is logarithmic rather than constant (although with a large base, usually 32-128). And, while you can make the non-mutating API identical to
dict
, the "mutating" API is obviously different. And, most of all, there's no HAMT batteries included with Python.2The
pyrsistent
library is a pretty solid implementation of HAMT-based dict-replacements (and various other types) for Python. It even has a nifty evolver API for porting existing mutating code to persistent code as smoothly as possible. But if you want to be explicit about returning copies rather than mutating, you just use it like this:That
d3 = d1.remove('a')
is exactly what the question is asking for.If you've got mutable data structures like
dict
andlist
embedded in thepmap
, you'll still have aliasing issues—you can only fix that by going immutable all the way down, embeddingpmap
s andpvector
s.1. HAMTs have also become popular in languages like Scala, Clojure, Haskell because they play very nicely with lock-free programming and software transactional memory, but neither of those is very relevant in Python.
2. In fact, there is an HAMT in the stdlib, used in the implementation of
contextvars
. The earlier withdrawn PEP explains why. But this is a hidden implementation detail of the library, not a public collection type.The del statement is what you're looking for. If you have a dictionary named foo with a key called 'bar', you can delete 'bar' from foo like this:
Note that this permanently modifies the dictionary being operated on. If you want to keep the original dictionary, you'll have to create a copy beforehand:
The
dict
call makes a shallow copy. If you want a deep copy, usecopy.deepcopy
.Here's a method you can copy & paste, for your convenience: