Path: csiph.com!usenet.pasdenom.info!goblin3!goblin.stu.neva.ru!rt.uk.eu.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'patterns': 0.04; 'case.': 0.05; 'mrab': 0.05; 'val': 0.07; 'dict': 0.09; 'key)': 0.09; 'visited': 0.09; 'def': 0.10; "wouldn't": 0.11; ':-)': 0.13; 'd[key]': 0.16; 'depth-first,': 0.16; 'indirect,': 0.16; 'key):': 0.16; 'key/value': 0.16; 'reliably': 0.16; 'sure.': 0.16; 'wrote:': 0.17; 'tim': 0.18; '>>>': 0.18; 'regardless': 0.21; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'setting': 0.26; 'checking': 0.27; 'first,': 0.27; 'guess': 0.27; '>>>>': 0.29; 'chase': 0.29; 'depth': 0.29; 'dictionary': 0.29; 'locks': 0.29; 'subject:some': 0.29; 'no,': 0.29; 'objects': 0.29; 'maybe': 0.29; 'problem.': 0.32; 'to:addr:python-list': 0.33; 'another': 0.33; 'list': 0.35; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'but': 0.36; 'level.': 0.36; "didn't": 0.36; 'should': 0.36; 'one,': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'delete': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'space': 0.39; 'list,': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'think': 0.40; 'most': 0.61; 'kind': 0.61; 'back': 0.62; 'more': 0.63; 'harder': 0.65; 'header:Reply-To:1': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; "'if": 0.84; 'angel': 0.93 Date: Mon, 17 Dec 2012 21:13:17 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org 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> <50CF8FBC.9000100@lightbird.net> <50CF95FB.6060800@davea.name> <50CFA5E7.7020701@mrabarnett.plus.com> In-Reply-To: <50CFA5E7.7020701@mrabarnett.plus.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:gf+/PdKifZ4+gk1JGxsQvCYi0ioChANHbMVVjpoLbHJ 5TGO/9Sfcp+s5qBEx8r7yCdWJtUDxExMMo00XTLGqciw7suc4k inhMhfxq9ntnKxjp06hCG28ezVC77oyqa7iYSGw78Nt2aIM4ED zn+cycy4h7ihjc6woA2b7VMrr+WrG0lPbHLKqzqbgilTOrw9/O kUJic/wtbe4BJNWh8ZO/h3dvhAYyl0dO74lkpjUutZYbAEcZwW SWu3h0G461zJMJ3iHQS6YbYH8AwQeRB5LlbLsHGu29py0xRqac 4paQELYx128RlwNuZKmS8u0PFTu1LYYrHDnKLiptkLDgHYQPg= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1355796826 news.xs4all.nl 6851 [2001:888:2000:d::a6]:40880 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:35034 On 12/17/2012 06:08 PM, MRAB wrote: > On 2012-12-17 22:00, Dave Angel wrote: >> On 12/17/2012 04:33 PM, Mitya Sirenef 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? >>> >> No, that would only cover the self-recursive case. If there's a dict >> which contains another one, which contains the first, then the recursion >> is indirect, and much harder to check for. >> >> Checking reliably for arbitrary recursion patterns is tricky, but >> do-able. Most people degenerate into just setting an arbitrary max >> depth. But I can describe two approaches to this kind of problem. >> > Wouldn't a set of the id of the visited objects work? Sure. But the set will get lots larger than a list, which is limited to the depth of max recursion. It also locks a lot more objects in memory, where the list only locks one per level. Now, maybe if the search is depth-first, and if you prune the set on the way back up, then it'll be space efficient. -- DaveA