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


Groups > comp.lang.python > #80561 > unrolled thread

Combining lists to dictionary

Started byDenis McMahon <denismfmcmahon@gmail.com>
First post2014-11-11 20:43 +0000
Last post2014-11-12 01:13 -0500
Articles 6 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  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

#80561 — Combining lists to dictionary

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-11-11 20:43 +0000
SubjectCombining lists to dictionary
Message-ID<m3tscl$f1e$2@dont-email.me>
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

[toc] | [next] | [standalone]


#80563

FromGary Herron <gherron@digipen.edu>
Date2014-11-11 12:55 -0800
Message-ID<mailman.15691.1415739909.18130.python-list@python.org>
In reply to#80561
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

[toc] | [prev] | [next] | [standalone]


#80567

FromRob Gaddi <rgaddi@technologyhighland.invalid>
Date2014-11-11 13:42 -0800
Message-ID<20141111134230.4a597eb6@rg.highlandtechnology.com>
In reply to#80561
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.

[toc] | [prev] | [next] | [standalone]


#80579

FromRoy Smith <roy@panix.com>
Date2014-11-11 20:06 -0500
Message-ID<roy-89A84F.20061711112014@news.panix.com>
In reply to#80561
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().

[toc] | [prev] | [next] | [standalone]


#80581

FromBen Finney <ben+python@benfinney.id.au>
Date2014-11-12 12:16 +1100
Message-ID<mailman.15704.1415755032.18130.python-list@python.org>
In reply to#80561
Denis McMahon <denismfmcmahon@gmail.com> 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

[toc] | [prev] | [next] | [standalone]


#80591

FromDave Angel <davea@davea.name>
Date2014-11-12 01:13 -0500
Message-ID<mailman.15714.1415772907.18130.python-list@python.org>
In reply to#80561
Ben Finney <ben+python@benfinney.id.au> Wrote in message:
> Denis McMahon <denismfmcmahon@gmail.com> 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)}

Or just z = dict(zip (x, y))
> 



-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web