RE: Dict comprehension help

2012-12-06 Thread Joseph L. Casale
>You could put the loop into a helper function, but if you are looping >through the same my_list more than once why not build a lookup table > >my_dict = {d["key"]: d for d in my_list} > >and then find the required dict with > >my_dict[value] I suppose, what I failed to clarify was that for each l

RE: Dict comprehension help

2012-12-06 Thread Peter Otten
Joseph L. Casale wrote: [Ian Kelly] >> {k: v for d in my_list if d['key'] == value for (k, v) in d.items()} > > Ugh, had part of that backwards:) Nice! > >> However, since you say that all dicts have a unique value for >> z['key'], you should never need to actually merge two dicts, correct? >> I

RE: Dict comprehension help

2012-12-05 Thread Joseph L. Casale
> {k: v for d in my_list if d['key'] == value for (k, v) in d.items()} Ugh, had part of that backwards:) Nice! > However, since you say that all dicts have a unique value for > z['key'], you should never need to actually merge two dicts, correct? > In that case, why not just use a plain for loop

Re: Dict comprehension help

2012-12-05 Thread Ian Kelly
On Wed, Dec 5, 2012 at 8:03 PM, Joseph L. Casale wrote: > I get a list of dicts as output from a source I need to then extract various > dicts > out of. I can easily extract the dict of choice based on it containing a key > with > a certain value using list comp but I was hoping to use dict comp