Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'say,': 0.05; 'function:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"private"': 0.16; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'namedtuple': 0.16; 'readable': 0.16; 'subject:Convert': 0.16; "{'a':": 0.16; 'wrote:': 0.18; 'bit': 0.19; '>>>': 0.22; 'cc:addr:python.org': 0.22; 'helper': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'this:': 0.26; 'header:In-Reply- To:1': 0.27; 'easier': 0.31; 'skip:d 20': 0.34; 'convert': 0.35; 'charset:us-ascii': 0.36; 'email addr:gmail.com': 0.63; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'received:50.22': 0.84 Date: Wed, 25 Sep 2013 18:41:25 -0500 From: Tim Chase To: tripsvt@gmail.com Subject: Re: Convert namedtuple to dictionary In-Reply-To: <635a3b46-3150-409f-8a9d-002af18fb734@googlegroups.com> References: <635a3b46-3150-409f-8a9d-002af18fb734@googlegroups.com> X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII 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 X-Get-Message-Sender-Via: boston.accountservergroup.com: none 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: 33 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380152379 news.xs4all.nl 15894 [2001:888:2000:d::a6]:60124 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54762 On 2013-09-25 15:45, tripsvt@gmail.com wrote: > Say, I have a namedtuple like this: > > {'a': brucelee(x=123, y=321), 'b': brucelee('x'=123, 'y'=321) > > I need to convert it to: > > {'a': {'x':123, 'y': 321},'b': {'x':123, 'y': 321}} While it uses the "private" member-variable "_fields", you can do >>> brucelee = namedtuple("brucelee", "x y") >>> d = {'a': brucelee(x=123,y=321), 'b': brucelee(x=234,y=432)} >>> dict((k, dict((s, getattr(v, s)) for s in v._fields)) for k,v in >>> d.iteritems()) {'a': {'y': 321, 'x': 123}, 'b': {'y': 432, 'x': 234}} which can be made a bit more readable with a helper function: >>> def dictify(some_named_tuple): ... return dict((s, getattr(some_named_tuple, s)) for s in some_named_tuple._fields) ... >>> dict((k, dictify(v)) for k,v in d.iteritems()) {'a': {'y': 321, 'x': 123}, 'b': {'y': 432, 'x': 234}} This would also make it easier to change/choose in the event "_fields" ever changes. -tkc