Dict comp help

Peter Otten __peter__ at web.de
Thu Jan 24 22:23:42 CET 2013

Joseph L. Casale wrote:

> Slightly different take on an old problem, I have a list of dicts, I need
> to build one dict from this based on two values from each dict in the
> list. Each of the dicts in the list have similar key names, but values of
> course differ.
> [{'a': 'xx', 'b': 'yy', 'c': 'zz'},  {'a': 'dd', 'b': 'ee', 'c': 'ff'}]
> { 'xx': 'zz', 'dd': 'ff'}
> Anyone have insight on how to pull this off?

>>> data = [{'a': 'xx', 'b': 'yy', 'c': 'zz'},  {'a': 'dd', 'b': 'ee', 'c': 
>>> {d["a"]: d["c"] for d in data}
{'xx': 'zz', 'dd': 'ff'}

More information about the Python-list mailing list