Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34998

Re: Delete dict and subdict items of some name

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 <msirenef@lightbird.net>
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; 'val': 0.07; 'key)': 0.09; 'python:': 0.09; 'def': 0.10; '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; 'regardless': 0.21; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'depth': 0.29; 'dictionary': 0.29; 'subject:some': 0.29; 'to:addr:python-list': 0.33; 'pm,': 0.35; 'something': 0.35; 'there': 0.35; 'should': 0.36; 'subject:: ': 0.38; 'delete': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40
Date Mon, 17 Dec 2012 12:43:58 -0500
From Mitya Sirenef <msirenef@lightbird.net>
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>
In-Reply-To <943dbf16-1cd1-4844-a666-d8863f5941f8@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.975.1355766242.29569.python-list@python.org> (permalink)
Lines 17
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1355766242 news.xs4all.nl 6974 [2001:888:2000:d::a6]:56866
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:34998

Show key headers only | View raw


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?
>
> -- Gnarlie

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)


-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Delete dict and subdict items of some name Gnarlodious <gnarlodious@gmail.com> - 2012-12-17 09:27 -0800
  Re: Delete dict and subdict items of some name Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-12-17 17:42 +0000
  Re: Delete dict and subdict items of some name Mitya Sirenef <msirenef@lightbird.net> - 2012-12-17 12:43 -0500
  Re: Delete dict and subdict items of some name Paul Rubin <no.email@nospam.invalid> - 2012-12-17 09:48 -0800
  Re: Delete dict and subdict items of some name Dave Angel <d@davea.name> - 2012-12-17 12:50 -0500
  Re: Delete dict and subdict items of some name Tim Chase <python.list@tim.thechases.com> - 2012-12-17 12:30 -0600
  Re: Delete dict and subdict items of some name Mitya Sirenef <msirenef@lightbird.net> - 2012-12-17 16:33 -0500
  Re: Delete dict and subdict items of some name Chris Angelico <rosuav@gmail.com> - 2012-12-18 08:53 +1100
  Re: Delete dict and subdict items of some name Dave Angel <d@davea.name> - 2012-12-17 17:00 -0500
  Re: Delete dict and subdict items of some name MRAB <python@mrabarnett.plus.com> - 2012-12-17 23:08 +0000
  Re: Delete dict and subdict items of some name Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-18 00:17 +0000
  Re: Delete dict and subdict items of some name Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-12-18 00:22 +0000
  Re: Delete dict and subdict items of some name Mitya Sirenef <msirenef@lightbird.net> - 2012-12-17 20:09 -0500
  Re: Delete dict and subdict items of some name Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2012-12-17 23:44 +0000
  Re: Delete dict and subdict items of some name Dave Angel <d@davea.name> - 2012-12-17 21:13 -0500
  Re: Delete dict and subdict items of some name Gnarlodious <gnarlodious@gmail.com> - 2012-12-17 21:30 -0800
    Re: Delete dict and subdict items of some name Hans Mulder <hansmu@xs4all.nl> - 2012-12-18 11:31 +0100
      Re: Delete dict and subdict items of some name Gnarlodious <gnarlodious@gmail.com> - 2012-12-18 07:27 -0800
        Re: Delete dict and subdict items of some name Terry Reedy <tjreedy@udel.edu> - 2012-12-18 11:29 -0500

csiph-web