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


Groups > comp.lang.python > #80567

Re: Combining lists to dictionary

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Rob Gaddi <rgaddi@technologyhighland.invalid>
Newsgroups comp.lang.python
Subject Re: Combining lists to dictionary
Date Tue, 11 Nov 2014 13:42:30 -0800
Organization Highland Technology, Inc.
Lines 32
Message-ID <20141111134230.4a597eb6@rg.highlandtechnology.com> (permalink)
References <m3tscl$f1e$2@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=US-ASCII
Content-Transfer-Encoding 7bit
Injection-Info mx02.eternal-september.org; posting-host="a48e804ab85e47f9d2e8748775b64aeb"; logging-data="15592"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gkDVjRtF1kqomRWgm/WS9"
X-Newsreader Claws Mail 3.9.3 (GTK+ 2.24.23; x86_64-pc-linux-gnu)
Cancel-Lock sha1:PXMGsn6WYGEQcB/Xcd0qJQo+ogo=
Xref csiph.com comp.lang.python:80567

Show key headers only | View raw


On Tue, 11 Nov 2014 20:43:01 +0000 (UTC)
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. 
> However the zip method handles the inequal length list problem. Granted 
> it would probably be advisable to check that x and y are the same length 
> before starting anyway.
> 
> -- 
> Denis McMahon, denismfmcmahon@gmail.com

To add to what Gary said, the explicit dict() init knows how to handle
an iterable that emits pairs of data.  So you can simplify further down to:

z = dict(zip(x, y))

If you're running Python2, and really that concerned about the extra
memory used by zip returning a list rather than an iterator, you can
use itertools.izip

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.

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