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


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

Re: Dict comp help

Started byOscar Benjamin <oscar.j.benjamin@gmail.com>
First post2013-01-24 21:11 +0000
Last post2013-01-24 21:11 +0000
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 Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-01-24 21:11 +0000

#37634 — Re: Dict comp help

FromOscar Benjamin <oscar.j.benjamin@gmail.com>
Date2013-01-24 21:11 +0000
SubjectRe: Dict comp help
Message-ID<mailman.1016.1359061894.2939.python-list@python.org>
On 24 January 2013 20:58, Joseph L. Casale <jcasale@activenetwerx.com> wrote:
> Hi,
> 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?
>

Your specification is not exactly clear about how to handle all of the
different cases or what you really want but how about:

>>> l = [{'a': 'xx', 'b': 'yy', 'c': 'zz'}, {'a': 'dd', 'b': 'ee', 'c': 'ff'}]
>>> dict(d.values()[:2] for d in l)
{'xx': 'zz', 'dd': 'ff'}


Oscar

[toc] | [standalone]


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


csiph-web