On Wed, Dec 5, 2012 at 8:03 PM, Joseph L. Casale <jcas...@activenetwerx.com> 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 so the > output > was not contained within a list. > > reduce(lambda x,y: dict(x.items() + y.items()), filter(lambda z: z['key'] == > value, my_list)) > > where my_list is a list of dicts. The premise is all dicts in the list have a > unique > value for z['key']. > > Anyone have a pointer as to how I might tackle this without lambdas and only > using dict comp?
{k: v for d in my_list if d['key'] == value for (k, v) in d.items()} 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 to search for the dict? for d in my_list: if d['key'] == value: result = d break else: raise ValueError("Value not found!") -- http://mail.python.org/mailman/listinfo/python-list