Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #80563
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!us.feeder.erje.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <gherron@digipen.edu> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.011 |
| X-Spam-Evidence | '*H*': 0.98; '*S*': 0.00; 'anyway.': 0.05; 'argument': 0.05; 'element': 0.07; 'elegant,': 0.09; 'raises': 0.09; 'python': 0.11; '(it': 0.16; 'argument.': 0.16; 'clunky': 0.16; 'dictionary,': 0.16; 'iterable': 0.16; 'iterator': 0.16; 'ought': 0.16; 'tuple': 0.16; ':-)': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'split': 0.19; 'seems': 0.21; 'handles': 0.22; 'header:User- Agent:1': 0.23; 'example.': 0.24; 'header:In-Reply-To:1': 0.27; 'coded': 0.31; 'continues': 0.31; 'gary': 0.31; 'keys': 0.31; 'tuples': 0.31; 'class': 0.32; 'lists': 0.32; 'probably': 0.32; 'problem.': 0.35; 'subject:lists': 0.35; 'but': 0.35; 'sequence': 0.36; 'method': 0.36; 'list': 0.37; 'starting': 0.37; 'to:addr :python-list': 0.38; 'list,': 0.38; 'pm,': 0.38; 'rather': 0.38; 'does': 0.39; 'to:addr:python.org': 0.39; 'length': 0.61; 'such': 0.63; 'zip': 0.64; 'more': 0.64; 'granted': 0.65; 'charset:windows-1252': 0.65; 'wish': 0.70; 'institute': 0.72; 'received:204': 0.75; 'dr.': 0.77; 'hand': 0.80 |
| Date | Tue, 11 Nov 2014 12:55:49 -0800 |
| From | Gary Herron <gherron@digipen.edu> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Combining lists to dictionary |
| References | <m3tscl$f1e$2@dont-email.me> |
| In-Reply-To | <m3tscl$f1e$2@dont-email.me> |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.15691.1415739909.18130.python-list@python.org> (permalink) |
| Lines | 45 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1415739909 news.xs4all.nl 2936 [2001:888:2000:d::a6]:40522 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:80563 |
Show key headers only | View raw
On 11/11/2014 12:43 PM, Denis McMahon 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.
>
Are you using python 2 or 3. (It ought to be 3 :-) .)
In Python3, zip does not create a list, but rather an iterator which
returns tuples. Not clunky at all, but rather an efficient
implementation of the loop which you hand coded in your other example.
From help(zip):
class zip(object)
| zip(iter1 [,iter2 [...]]) --> zip object
|
| Return a zip object whose .__next__() method returns a tuple where
| the i-th element comes from the i-th iterable argument. The
.__next__()
| method continues until the shortest iterable in the argument sequence
| is exhausted and then it raises StopIteration.
...
Gary Herron
--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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