The Python Oracle

Delete an element from a dictionary

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay

--

Chapters
00:00 Question
00:23 Accepted answer (Score 2403)
01:31 Answer 2 (Score 485)
01:49 Answer 3 (Score 115)
02:13 Answer 4 (Score 103)
06:21 Thank you

--

Full question
https://stackoverflow.com/questions/5844...

Accepted answer links:
[del]: http://docs.python.org/reference/simple_...
[copy]: https://docs.python.org/library/copy.htm...
[this answer]: https://stackoverflow.com/a/50341031/908...

Answer 2 links:
[pop]: https://docs.python.org/library/stdtypes...

Answer 4 links:
[dict.pop()]: https://docs.python.org/3/library/stdtyp...
[del]: https://docs.python.org/3/tutorial/datas...
[KeyError]: https://docs.python.org/3/library/except...
[copy.deepcopy()]: https://docs.python.org/3/library/copy.h...
[copy.copy()]: https://docs.python.org/3/library/copy.h...
[dict.copy()]: https://docs.python.org/3/library/stdtyp...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #dictionary #del

#avk47



ACCEPTED ANSWER

Score 2531


The del statement removes an element:

del d[key]

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:

def removekey(d, key):
    r = dict(d)
    del r[key]
    return r

The dict() constructor makes a shallow copy. To make a deep copy, see the copy module.


Note that making a copy for every dict del/assignment/etc. means you're going from constant time to linear time, and also using linear space. For small dicts, this is not a problem. But if you're planning to make lots of copies of large dicts, you probably want a different data structure, like a HAMT (as described in this answer).




ANSWER 2

Score 518


pop mutates the dictionary.

 >>> lol = {"hello": "gdbye"}
 >>> lol.pop("hello")
     'gdbye'
 >>> lol
     {}

If you want to keep the original you could just copy it.




ANSWER 3

Score 118


I think your solution is best way to do it. But if you want another solution, you can create a new dictionary with using the keys from old dictionary without including your specified key, like this:

>>> a
{0: 'zero', 1: 'one', 2: 'two', 3: 'three'}
>>> {i:a[i] for i in a if i!=0}
{1: 'one', 2: 'two', 3: 'three'}



ANSWER 4

Score 83


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:

del foo['bar']

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:

>>> foo = {'bar': 'baz'}
>>> fu = dict(foo)
>>> del foo['bar']
>>> print foo
{}
>>> print fu
{'bar': 'baz'}

The dict call makes a shallow copy. If you want a deep copy, use copy.deepcopy.

Here's a method you can copy & paste, for your convenience:

def minus_key(key, dictionary):
    shallow_copy = dict(dictionary)
    del shallow_copy[key]
    return shallow_copy