On 9/25/13 6:45 PM, trip...@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]}

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to