Re: Convert namedtuple to dictionary

2013-09-26 Thread Virendra Tripathi
Thanks guys! BTW, please ignore the follow-up question there. I had written a script earlier which had a dict like: {'a':[x=10, y=23,..], 'b': [x=10, y=23,…],…..} I was thinking about that and therefore the error. Apologies. -- https://mail.python.org/mailman/listinfo/python-list

Re: Convert namedtuple to dictionary

2013-09-26 Thread Ned Batchelder
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 stuc

Re: Convert namedtuple to dictionary

2013-09-26 Thread Ned Batchelder
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

Re: Convert namedtuple to dictionary

2013-09-26 Thread Tim Chase
On 2013-09-26 16:42, Virendra Tripathi wrote: > Thx Tim. Your solution works. After Steven's reply, I recommend dict((k,v._asdict()) for k,v in d.iteritems()) which simplifies matters. -tkc -- https://mail.python.org/mailman/listinfo/python-list

Re: Convert namedtuple to dictionary

2013-09-26 Thread Virendra Tripathi
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. Thanks, Virendra

Re: Convert namedtuple to dictionary

2013-09-26 Thread Virendra Tripathi
Hi MRAB, Thanks. Learning a lot thanks to you guys. I should have mentioned that the data is dynamic, in the sense that 'brucelee' could be 'jackiechan' , the number of named tuples - i.e., 'brucelee(…)', could change as well, the next time data is pulled in. The format of the data would howev

Re: Convert namedtuple to dictionary

2013-09-26 Thread Virendra Tripathi
Thx Tim. Your solution works. I am, as you can probably tell, a newbie at Python. -- https://mail.python.org/mailman/listinfo/python-list

Re: Convert namedtuple to dictionary

2013-09-26 Thread Tim Chase
On 2013-09-26 01:08, Steven D'Aprano wrote: > On Wed, 25 Sep 2013 18:41:25 -0500, Tim Chase wrote about > namedtuple: > > > While it uses the "private" member-variable "_fields", you can do > > It's not actually private! > > namedtuple is documented as an exception to the rule that methods > st

Re: Convert namedtuple to dictionary

2013-09-25 Thread Peter Otten
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 w

Re: Convert namedtuple to dictionary

2013-09-25 Thread Steven D'Aprano
On Wed, 25 Sep 2013 15:45:49 -0700, tripsvt wrote: > Need suggestions. > > Say, I have a namedtuple like this: > > {'a': brucelee(x=123, y=321), 'b': brucelee('x'=123, 'y'=321) That's not a namedtuple, that's a dict containing two namedtuples. > I need to convert it to: > > {'a': {'x':123,

Re: Convert namedtuple to dictionary

2013-09-25 Thread Terry Reedy
On 9/25/2013 9:15 PM, MRAB wrote: On 26/09/2013 02:08, Steven D'Aprano wrote: On Wed, 25 Sep 2013 18:41:25 -0500, Tim Chase wrote about namedtuple: While it uses the "private" member-variable "_fields", you can do It's not actually private! namedtuple is documented as an exception to the ru

Re: Convert namedtuple to dictionary

2013-09-25 Thread MRAB
On 26/09/2013 02:08, Steven D'Aprano wrote: On Wed, 25 Sep 2013 18:41:25 -0500, Tim Chase wrote about namedtuple: While it uses the "private" member-variable "_fields", you can do It's not actually private! namedtuple is documented as an exception to the rule that methods starting with a sin

Re: Convert namedtuple to dictionary

2013-09-25 Thread Steven D'Aprano
On Wed, 25 Sep 2013 18:41:25 -0500, Tim Chase wrote about namedtuple: > While it uses the "private" member-variable "_fields", you can do It's not actually private! namedtuple is documented as an exception to the rule that methods starting with a single leading underscore are private. Named tup

Re: Convert namedtuple to dictionary

2013-09-25 Thread Ned Batchelder
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

Re: Convert namedtuple to dictionary

2013-09-25 Thread MRAB
On 25/09/2013 23:45, 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 assume you mean: {'a': brucelee(x=123, y=321), 'b': brucelee(x=123, y=321)} I need to convert it to: {'a': {'x':123, 'y': 32

Re: Convert namedtuple to dictionary

2013-09-25 Thread Tim Chase
On 2013-09-25 15:45, trip...@gmail.com wrote: > 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}} While it uses the "private" member-variable "_fields", you c

Convert namedtuple to dictionary

2013-09-25 Thread tripsvt
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