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


Groups > comp.lang.python > #80579

Re: Combining lists to dictionary

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!bloom-beacon.mit.edu!bloom-beacon.mit.edu!panix!roy
From Roy Smith <roy@panix.com>
Newsgroups comp.lang.python
Subject Re: Combining lists to dictionary
Date Tue, 11 Nov 2014 20:06:47 -0500
Organization PANIX Public Access Internet and UNIX, NYC
Lines 17
Message-ID <roy-89A84F.20061711112014@news.panix.com> (permalink)
References <m3tscl$f1e$2@dont-email.me>
NNTP-Posting-Host localhost
X-Trace reader1.panix.com 1415754407 5129 127.0.0.1 (12 Nov 2014 01:06:47 GMT)
X-Complaints-To abuse@panix.com
NNTP-Posting-Date Wed, 12 Nov 2014 01:06:47 +0000 (UTC)
User-Agent MT-NewsWatcher/3.5.3b3 (Intel Mac OS X)
Xref csiph.com comp.lang.python:80579

Show key headers only | View raw


In article <m3tscl$f1e$2@dont-email.me>,
 Denis McMahon <denismfmcmahon@gmail.com> wrote:

> 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)}
> z = {x[n]:y[n] for n in range(min(len(x),len(y)))}
> 
> 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 would definitely use the first one.  It's so much easier to read.  If 
I was worried that the lists might be so long that the cost of building 
the intermediate list mattered, I'd use izip() instead of zip().

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Combining lists to dictionary Denis McMahon <denismfmcmahon@gmail.com> - 2014-11-11 20:43 +0000
  Re: Combining lists to dictionary Gary Herron <gherron@digipen.edu> - 2014-11-11 12:55 -0800
  Re: Combining lists to dictionary Rob Gaddi <rgaddi@technologyhighland.invalid> - 2014-11-11 13:42 -0800
  Re: Combining lists to dictionary Roy Smith <roy@panix.com> - 2014-11-11 20:06 -0500
  Re: Combining lists to dictionary Ben Finney <ben+python@benfinney.id.au> - 2014-11-12 12:16 +1100
  Re: Combining lists to dictionary Dave Angel <davea@davea.name> - 2014-11-12 01:13 -0500

csiph-web