Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!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.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'see.': 0.07; 'elegant,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'caveat': 0.16; 'clunky': 0.16; 'dictionary,': 0.16; 'finney': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'split': 0.19; 'seems': 0.21; 'header:User-Agent:1': 0.23; 'fine': 0.24; 'least': 0.26; 'primary': 0.26; 'header:X-Complaints-To:1': 0.27; 'keys': 0.31; 'tuples': 0.31; 'writes:': 0.31; 'lists': 0.32; 'compatible': 0.32; 'noticed': 0.34; 'classes': 0.35; 'one,': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'method': 0.36; 'too': 0.37; 'list': 0.37; 'ben': 0.38; 'rich': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'most': 0.60; 'such': 0.63; 'zip': 0.64; 'more': 0.64; 'wish': 0.70; 'gain': 0.79; 'age': 0.80; 'received:125': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Re: Combining lists to dictionary Date: Wed, 12 Nov 2014 12:16:59 +1100 References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:kWrqzD3r6K3btJbG6mwEd8xrK40= 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1415755032 news.xs4all.nl 2860 [2001:888:2000:d::a6]:42943 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:80581 Denis McMahon writes: > Hi > > Given x,y are a lists of keys and value that I wish to combine to a > dictionary, such that x[n] is the key for value y[n], which is preferred: > > z = {a:b for (a,b) in zip(x,y)} This one, with the caveat to use PEP-8 compatible formatting:: z = {a: b for (a, b) in zip(x, y)} > z = {x[n]:y[n] for n in range(min(len(x),len(y)))} Too much indirection for no gain that I can see. > The zip feels more elegant, but it seems clunky to use the zip method to > create a list of tuples just to split them up into key:value pairs. I think ‘zip’ has this as a primary intended purpose, so it seems fine to me. -- \ “Of all classes the rich are the most noticed and the least | `\ studied.” —John Kenneth Galbraith, _The Age of Uncertainty_, | _o__) 1977 | Ben Finney