On Tue, Dec 18, 2012 at 8:33 AM, Mitya Sirenef <msire...@lightbird.net> wrote: > On 12/17/2012 01:30 PM, Tim Chase wrote: >> >> 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 >> >> :-) > > > True -- didn't think of that..! > > I guess then adding a check 'if val is not d: delkey(val, key)' > would take care of it?
Nope, recursion could occur anywhere. You'd have to maintain a set of "visited nodes" (or their id()s, same diff), and skip any that are in it. ChrisA -- http://mail.python.org/mailman/listinfo/python-list