Delete dict and subdict items of some name

Tim Chase python.list at
Mon Dec 17 19:30:16 CET 2012

On 12/17/12 11:43, Mitya Sirenef wrote:
> On 12/17/2012 12:27 PM, Gnarlodious wrote:
>> Hello. What I want to do is delete every dictionary key/value
>> of the name 'Favicon' regardless of depth in subdicts, of which
>> there are many. What is the best way to do it?
> Something like this should work:
> def delkey(d, key):
>      if isinstance(d, dict):
>          if key in d: del d[key]
>          for val in d.values():
>              delkey(val, key)

Unless you have something hatefully recursive like

  d = {}
  d["hello"] = d


However, assuming the initial structure is tree-ish (acyclic),
Mitya's function should do the trick


More information about the Python-list mailing list