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


Groups > comp.lang.python > #54768

Re: Convert namedtuple to dictionary

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <nedbat@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'say,': 0.05; 'method.': 0.07; 'suggestions.': 0.09; 'cc:addr:python-list': 0.11; 'follow- up': 0.16; 'namedtuple': 0.16; 'namedtuples': 0.16; 'subject:Convert': 0.16; 'values?': 0.16; "{'a':": 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'later': 0.20; 'cc:addr:python.org': 0.22; 'header:User-Agent:1': 0.23; 'question': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'this:': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'easier': 0.31; 'convert': 0.35; 'received:google.com': 0.35; 'list': 0.37; 'pm,': 0.38; 'email addr:gmail.com': 0.63; 'to:addr:gmail.com': 0.65
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; bh=TpqPajrM2NTPKSGgMKlghvw+6T1qmL6RfnTusNx5h88=; b=yTRNMOMBAAv1S0aeup8UgDV7NsiKMZXh90jeRxeK4wWhrkDLIFbA0cs5ysg+B7v+/e oUVSxKU4EY7oJsaJspDZ4hZYJs4hNqp9NZP3ToInXGhrFP1SeGzRr16FUrvd46iADzNa 01BBzaUd8B41JhTbuCgP+MfGEeiwAI20a0a6/bbdefEhPcTe6MHmNiOS+DPlN2QfZ94W xNk6zXxPUyniFmWny4KXfDPqsFDH7YdZfSBkyXhniZf5gGjklnhl3nwKqczj/49O+q6o Cc8BJ7CmKdqPoBaRELswhPNHXJ43KA4oJu+3ZD4E4P+dTQok9y9FubE6ycG2b3HR8PFw oCuw==
X-Received by 10.236.32.3 with SMTP id n3mr8981673yha.25.1380154550679; Wed, 25 Sep 2013 17:15:50 -0700 (PDT)
Sender Ned Batchelder <nedbat@gmail.com>
Date Wed, 25 Sep 2013 20:15:45 -0400
From Ned Batchelder <ned@nedbatchelder.com>
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20130801 Thunderbird/17.0.8
MIME-Version 1.0
To tripsvt@gmail.com
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
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 <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.328.1380154894.18130.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1380154894 news.xs4all.nl 15872 [2001:888:2000:d::a6]:50650
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:54768

Show key headers only | View raw


On 9/25/13 6:45 PM, 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 need to convert it to:
>
> {'a': {'x':123, 'y': 321},'b': {'x':123, 'y': 321}}

Namedtuples have a ._asdict() method. You can convert your dictionary of 
namedtuples (let's call it tupledict) like this:

     dictdict = { k: nt._asdict() for k, nt in tupledict }

--Ned.
> 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]}

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


Thread

Convert namedtuple to dictionary tripsvt@gmail.com - 2013-09-25 15:45 -0700
  Re: Convert namedtuple to dictionary Tim Chase <python.list@tim.thechases.com> - 2013-09-25 18:41 -0500
    Re: Convert namedtuple to dictionary Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-09-26 01:08 +0000
      Re: Convert namedtuple to dictionary MRAB <python@mrabarnett.plus.com> - 2013-09-26 02:15 +0100
      Re: Convert namedtuple to dictionary Terry Reedy <tjreedy@udel.edu> - 2013-09-25 21:45 -0400
      Re: Convert namedtuple to dictionary Tim Chase <python.list@tim.thechases.com> - 2013-09-26 06:51 -0500
  Re: Convert namedtuple to dictionary MRAB <python@mrabarnett.plus.com> - 2013-09-26 00:52 +0100
  Re: Convert namedtuple to dictionary Ned Batchelder <ned@nedbatchelder.com> - 2013-09-25 20:15 -0400
  Re: Convert namedtuple to dictionary Steven D'Aprano <steve@pearwood.info> - 2013-09-26 03:48 +0000
  Re: Convert namedtuple to dictionary Peter Otten <__peter__@web.de> - 2013-09-26 08:47 +0200

csiph-web