Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:help': 0.07; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'slightly': 0.15; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; "{'a':": 0.16; 'wrote:': 0.17; '>>>': 0.18; 'header:User- Agent:1': 0.26; 'values': 0.26; 'header:X-Complaints-To:1': 0.28; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'list': 0.35; 'problem,': 0.35; 'similar': 0.35; 'list.': 0.35; 'received:org': 0.36; 'but': 0.36; 'two': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'build': 0.39; 'header:Received:5': 0.40; 'different': 0.63; 'insight': 0.71; 'subject:comp': 0.96 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Dict comp help Date: Thu, 24 Jan 2013 22:23:42 +0100 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b198.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359062623 news.xs4all.nl 6867 [2001:888:2000:d::a6]:55665 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37636 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'}