Re: List comp help

2013-07-14 Thread Terry Reedy
On 7/14/2013 1:10 PM, Joseph L. Casale wrote: I have a dict of lists. I need to create a list of 2 tuples, where each tuple is a key from the dict with one of the keys list items. my_dict = { 'key_a': ['val_a', 'val_b'], 'key_b': ['val_c'], 'key_c': [] } [(k, x) for k, v in my_di

Re: List comp help

2013-07-14 Thread rurpy
On Sunday, July 14, 2013 12:32:34 PM UTC-6, ru...@yahoo.com wrote: > Or more simply: > [(k, v or None) for k, v in my_dict.items()] Too simply :-( Didn't read the op carefully enough. Sorry. -- http://mail.python.org/mailman/listinfo/python-list

Re: List comp help

2013-07-14 Thread rurpy
On 07/14/2013 11:16 AM, Chris Angelico wrote: > On Mon, Jul 15, 2013 at 3:10 AM, Joseph L. Casale > wrote: >> I have a dict of lists. I need to create a list of 2 tuples, where each >> tuple is a key from >> the dict with one of the keys list items. >> >> my_dict = { >> 'key_a': ['val_a', 'va

RE: List comp help

2013-07-14 Thread Joseph L. Casale
> Yeah, it's remarkably easy too! Try this: > > [(k, x) for k, v in my_dict.items() for x in v or [None]] > > An empty list counts as false, so the 'or' will then take the second option, > and iterate over the one-item list with > > None in it. Right, I overlooked that! Much appreciated, jlc --

Re: List comp help

2013-07-14 Thread Chris Angelico
On Mon, Jul 15, 2013 at 3:10 AM, Joseph L. Casale wrote: > I have a dict of lists. I need to create a list of 2 tuples, where each tuple > is a key from > the dict with one of the keys list items. > > my_dict = { > 'key_a': ['val_a', 'val_b'], > 'key_b': ['val_c'], > 'key_c': [] > } >

List comp help

2013-07-14 Thread Joseph L. Casale
I have a dict of lists. I need to create a list of 2 tuples, where each tuple is a key from the dict with one of the keys list items. my_dict = { 'key_a': ['val_a', 'val_b'], 'key_b': ['val_c'], 'key_c': [] } [(k, x) for k, v in my_dict.items() for x in v] This works, but I need to t