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's another variation using list comprehension:
The approach is based on an answer from this post: Efficient way to remove keys with empty strings from a dict
There're a lot of nice answers, but I want to emphasize one thing.
You can use both
dict.pop()
method and a more genericdel
statement to remove items from a dictionary. They both mutate the original dictionary, so you need to make a copy (see details below).And both of them will raise a
KeyError
if the key you're providing to them is not present in the dictionary:and
You have to take care of this:
by capturing the exception:
and
by performing a check:
and
but with
pop()
there's also a much more concise way - provide the default return value:Unless you use
pop()
to get the value of a key being removed you may provide anything, not necessaryNone
. Though it might be that usingdel
within
check is slightly faster due topop()
being a function with its own complications causing overhead. Usually it's not the case, sopop()
with default value is good enough.As for the main question, you'll have to make a copy of your dictionary, to save the original dictionary and have a new one without the key being removed.
Some other people here suggest making a full (deep) copy with
copy.deepcopy()
, which might be an overkill, a "normal" (shallow) copy, usingcopy.copy()
ordict.copy()
, might be enough. The dictionary keeps a reference to the object as a value for a key. So when you remove a key from a dictionary this reference is removed, not the object being referenced. The object itself may be removed later automatically by the garbage collector, if there're no other references for it in the memory. Making a deep copy requires more calculations compared to shallow copy, so it decreases code performance by making the copy, wasting memory and providing more work to the GC, sometimes shallow copy is enough.However, if you have mutable objects as dictionary values and plan to modify them later in the returned dictionary without the key, you have to make a deep copy.
With shallow copy:
With deep copy:
pop
mutates the dictionary.If you want to keep the original you could just copy it.
Simply call del d['key'].
However, in production, it is always a good practice to check if 'key' exists in d.
this doesn't do any error handling, it assumes the key is in the dict, you might want to check that first and
raise
if its notNo, there is no other way than
However, often creating copies of only slightly altered dictionaries is probably not a good idea because it will result in comparatively large memory demands. It is usually better to log the old dictionary(if even necessary) and then modify it.