Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37636 > unrolled thread

Re: Dict comp help

Started byPeter Otten <__peter__@web.de>
First post2013-01-24 22:23 +0100
Last post2013-01-24 22:23 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Dict comp help Peter Otten <__peter__@web.de> - 2013-01-24 22:23 +0100

#37636 — Re: Dict comp help

FromPeter Otten <__peter__@web.de>
Date2013-01-24 22:23 +0100
SubjectRe: Dict comp help
Message-ID<mailman.1017.1359062623.2939.python-list@python.org>
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': 
'ff'}]
>>> {d["a"]: d["c"] for d in data}
{'xx': 'zz', 'dd': 'ff'}

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web