Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'val': 0.07; 'key)': 0.09; 'python:': 0.09; 'to:addr:python.list': 0.09; 'to:addr:tim.thechases.com': 0.09; 'to:name:tim chase': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; ':-)': 0.13; 'd[key]': 0.16; 'key):': 0.16; 'key/value': 0.16; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'wrote:': 0.17; 'tim': 0.18; '>>>': 0.18; 'regardless': 0.21; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'cc:addr:python.org': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'guess': 0.27; 'chase': 0.29; 'depth': 0.29; 'dictionary': 0.29; 'subject:some': 0.29; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; "didn't": 0.36; 'should': 0.36; 'subject:: ': 0.38; 'delete': 0.38; 'received:192': 0.39; 'received:192.168': 0.40; 'think': 0.40; "'if": 0.84 Date: Mon, 17 Dec 2012 16:33:48 -0500 From: Mitya Sirenef User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: Tim Chase Subject: Re: Delete dict and subdict items of some name References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> <50CF64B8.6010305@tim.thechases.com> In-Reply-To: <50CF64B8.6010305@tim.thechases.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 31 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355780033 news.xs4all.nl 6910 [2001:888:2000:d::a6]:58372 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35017 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? -m -- Lark's Tongue Guide to Python: http://lightbird.net/larks/