Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'val': 0.07; 'key)': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; ':-)': 0.13; '-tkc': 0.16; 'd[key]': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'key):': 0.16; 'key/value': 0.16; 'message-id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'wrote:': 0.17; 'regardless': 0.21; 'assuming': 0.22; '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; 'initial': 0.28; 'depth': 0.29; 'dictionary': 0.29; 'subject:some': 0.29; 'function': 0.30; 'structure': 0.32; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'should': 0.36; 'subject:: ': 0.38; 'delete': 0.38; 'received:50.22': 0.84 Date: Mon, 17 Dec 2012 12:30:16 -0600 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: Mitya Sirenef Subject: Re: Delete dict and subdict items of some name References: <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com> <50CF59DE.6080608@lightbird.net> In-Reply-To: <50CF59DE.6080608@lightbird.net> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355768960 news.xs4all.nl 6879 [2001:888:2000:d::a6]:53047 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35002 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 -tkc