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}} > > 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]}
One more: >>> from collections import namedtuple >>> brucelee = namedtuple("brucelee", "x y") >>> def to_dict(n): ... return dict(zip(n._fields, n)) ... >>> data = {'a': brucelee(x=123, y=321), 'b': brucelee(x=123, y=321)} >>> {k: to_dict(v) for k, v in data.items()} {'a': {'y': 321, 'x': 123}, 'b': {'y': 321, 'x': 123}} -- https://mail.python.org/mailman/listinfo/python-list