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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'say,': 0.05; 'suggestions.': 0.09; 'assume': 0.14; 'dict': 0.16; 'follow-up': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'namedtuple': 0.16; 'subject:Convert': 0.16; 'values?': 0.16; "{'a':": 0.16; 'wrote:': 0.18; 'later': 0.20; '>>>': 0.22; 'putting': 0.22; 'header:User-Agent:1': 0.23; 'finally,': 0.24; 'question': 0.24; 'this:': 0.26; 'pass': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'easier': 0.31; 'convert': 0.35; 'list': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'email addr:gmail.com': 0.63; 'field': 0.63; 'zip': 0.64; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'reply-to:addr:python.org': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MqNrtQqe c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=0kkAYlmtguIA:10 a=uPasbX5kAJEA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=HgPT_0boHaYA:10 a=pGLkceISAAAA:8 a=PPs1Lhr7MLKyzVOsDKYA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Thu, 26 Sep 2013 00:52:32 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20130801 Thunderbird/17.0.8 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Convert namedtuple to dictionary References: <635a3b46-3150-409f-8a9d-002af18fb734@googlegroups.com> In-Reply-To: <635a3b46-3150-409f-8a9d-002af18fb734@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 Reply-To: python-list@python.org 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1380153150 news.xs4all.nl 15974 [2001:888:2000:d::a6]:34863 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54763 On 25/09/2013 23:45, tripsvt@gmail.com wrote: > Need suggestions. > > Say, I have a namedtuple like this: > > {'a': brucelee(x=123, y=321), 'b': brucelee('x'=123, 'y'=321) I assume you mean: {'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}} > You can get the field names using ._fields and the values by using list(...): >>> n = brucelee(x=123, y=321) >>> n._fields ('x', 'y') >>> list(n) [123, 321] Zip then together and pass the result to dict: >>> dict(zip(n._fields, list(n))) {'x': 123, 'y': 321} And, finally, putting that in a dict comprehension: >>> n = {'a': brucelee(x=123, y=321), 'b': brucelee(x=123, y=321)} >>> {k: dict(zip(v._fields, list(v))) for k, v in n.items()} {'a': {'x': 123, 'y': 321}, 'b': {'x': 123, 'y': 321}} > Follow-up question -- > > Which would be easier to work with if I had to later extract/manipulate the 'x', 'y' values? The format (dicts) above or a list of values like this: > > {'a': ['x':123, 'y': 321],'b': ['x':123, 'y': 321]} > That's not valid Python!