Convert namedtuple to dictionary
Ned Batchelder
ned at nedbatchelder.com
Thu Sep 26 14:38:26 EDT 2013
On 9/26/13 1:42 PM, Ned Batchelder wrote:
> 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 }
>
Oops:
dictdict = { k: nt._asdict() for k, nt in tupledict.iteritems() }
(sorry!)
--Ned.
>> Thanks,
>>
>> Virendra
>
More information about the Python-list
mailing list