Convert namedtuple to dictionary

Ned Batchelder ned at nedbatchelder.com
Thu Sep 26 13:42:59 EDT 2013


On 9/26/13 1:17 PM, Virendra Tripathi wrote:
> Hi Ned,
>
> Thanks. Wouldn't I have to first create a function to pull out the 'dictdict' from the data? I assume 'dictdict' refers to the 'brucelee' named tuple in the example. That's where I was getting stuck-trying to pull out the named tuples from the dict and operate on it.
I assumed you already had the data you showed.  This is tupledict:

     {'a': brucelee(x=123, y=321), 'b': brucelee('x'=123, 'y'=321)

Given that, you create dictdict with the line I showed:

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

> Thanks,
>
> Virendra




More information about the Python-list mailing list