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: Denis McMahon Newsgroups: comp.lang.python Subject: Combining lists to dictionary Date: Tue, 11 Nov 2014 20:43:01 +0000 (UTC) Organization: A noiseless patient Spider Lines: 16 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Date: Tue, 11 Nov 2014 20:43:01 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="66ffcfa4470a58bcddbdcd1913f98ab4"; logging-data="15406"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+HXJgeA+HFr5OFxPhCQ52QbvC0LeBpM54=" User-Agent: Pan/0.136 (I'm far too busy being delicious; GIT 926a150 git://git.gnome.org/pan2) Cancel-Lock: sha1:dH640Cp9lWLO3ytxgZ/I520Dypg= Xref: csiph.com comp.lang.python:80561 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