Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Faster (smarter?) dictionary building

0 views
Skip to first unread message

Michael T. Babcock

unread,
Oct 30, 2003, 2:45:48 PM10/30/03
to pytho...@python.org
I have a list of column headings and an array of values:
headings, values = ['a', 'b', 'c'], [1, 2, 3]

I want to construct a dictionary such that d['a'] = 1 and so on.

The first way I tried to do this was:

d = {}
for h,v in headings, values:
d[h] = v

It turns out this doesn't work, but it was worth a try. I ended up
falling back on the more C-like:

for i in range(len(headings)):
d[h[i]] = v[i]

Is there anything somewhat cleaner or more pythonesque I could do
instead? Thanks.

--
Michael T. Babcock
C.T.O., FibreSpeed Ltd.
http://www.fibrespeed.net/~mbabcock

Tim Peters

unread,
Oct 30, 2003, 2:57:25 PM10/30/03
to pytho...@python.org
[Michael T. Babcock]

> I have a list of column headings and an array of values:
> headings, values = ['a', 'b', 'c'], [1, 2, 3]
>
> I want to construct a dictionary such that d['a'] = 1 and so on.
>
> The first way I tried to do this was:
>
> d = {}
> for h,v in headings, values:
> d[h] = v
>
> It turns out this doesn't work, but it was worth a try. I ended up
> falling back on the more C-like:
>
> for i in range(len(headings)):
> d[h[i]] = v[i]
>
> Is there anything somewhat cleaner or more pythonesque I could do
> instead?

In sufficiently recent versions of Python (>= 2.2),

>>> headings, values = ['a', 'b', 'c'], [1, 2, 3]

>>> dict(zip(headings, values))
{'a': 1, 'c': 3, 'b': 2}
>>>

Looking at the value of the anonymous nested expression should make it
clearer at first:

>>> zip(headings, values)
[('a', 1), ('b', 2), ('c', 3)]
>>>

Picture a zipper, then squint <wink>.


David Eppstein

unread,
Oct 30, 2003, 3:24:23 PM10/30/03
to
In article <mailman.259.1067543...@python.org>,

"Michael T. Babcock" <mbab...@fibrespeed.net> wrote:

> I have a list of column headings and an array of values:
> headings, values = ['a', 'b', 'c'], [1, 2, 3]
>
> I want to construct a dictionary such that d['a'] = 1 and so on.

d = dict(zip(headings,values))

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science

Jay Dorsey

unread,
Oct 30, 2003, 2:46:25 PM10/30/03
to pytho...@python.org
Michael T. Babcock wrote:
> I have a list of column headings and an array of values:
> headings, values = ['a', 'b', 'c'], [1, 2, 3]
>
> I want to construct a dictionary such that d['a'] = 1 and so on.
>

How about:

>>> headings, values = ['a', 'b', 'c'], [1, 2, 3]

>>> d = {}
>>> for h, v in zip(headings, values):
... d[h] = v
...
>>> d


{'a':1, 'c':3, 'b':2}

Jay


Bengt Richter

unread,
Oct 30, 2003, 5:19:41 PM10/30/03
to
On Thu, 30 Oct 2003 14:45:48 -0500, "Michael T. Babcock" <mbab...@fibrespeed.net> wrote:

>I have a list of column headings and an array of values:
>headings, values = ['a', 'b', 'c'], [1, 2, 3]
>
>I want to construct a dictionary such that d['a'] = 1 and so on.
>
>The first way I tried to do this was:
>
>d = {}
>for h,v in headings, values:
> d[h] = v
>
>It turns out this doesn't work, but it was worth a try. I ended up

IMO it would be interesting to make that work, but spelling it like a tuple unpacking
assignment with a 'for' in front of it, to make it step through the sequences on the right. E.g,

d = {}
for h,v = headings, values: # illegal now, proposed lazy parallel sequence unpacking
d[h] = v

would work as "expected", i.e., as if

for h,v in itertools.izip(headings,values):
d[h] = v

Regards,
Bengt Richter

Javier Ruere

unread,
Oct 30, 2003, 10:44:26 PM10/30/03
to Michael T. Babcock, pytho...@python.org
Michael T. Babcock wrote:

> I have a list of column headings and an array of values:
> headings, values = ['a', 'b', 'c'], [1, 2, 3]
>
> I want to construct a dictionary such that d['a'] = 1 and so on.
>
> The first way I tried to do this was:
>
> d = {}
> for h,v in headings, values:
> d[h] = v
>
> It turns out this doesn't work, but it was worth a try. I ended up

> falling back on the more C-like:
>
> for i in range(len(headings)):
> d[h[i]] = v[i]
>
> Is there anything somewhat cleaner or more pythonesque I could do

> instead? Thanks.
>
How about this:

>>> a = [ 1,2,3]
>>> b = [ 'a', 'b', 'c' ]
>>> dict(zip(a,b))
{1: 'a', 2: 'b', 3: 'c'}
>>> dict(zip(b,a))


{'a': 1, 'c': 3, 'b': 2}


Javier

Michael T. Babcock

unread,
Nov 3, 2003, 8:36:55 AM11/3/03
to pytho...@python.org

> Subject:
> Re: Faster (smarter?) dictionary building
> From:
> David Eppstein <epps...@ics.uci.edu>
> Date:
> Thu, 30 Oct 2003 12:24:23 -0800
> To:
> pytho...@python.org
>
>
>In article <mailman.259.1067543...@python.org>,

> "Michael T. Babcock" <mbab...@fibrespeed.net> wrote:
>
>
>
>>I have a list of column headings and an array of values:
>>headings, values = ['a', 'b', 'c'], [1, 2, 3]
>>
>>I want to construct a dictionary such that d['a'] = 1 and so on.
>>
>>
>
>d = dict(zip(headings,values))
>
>
>

Beautiful; thank-you very much. I was sure my code looked uglier than
necessary.

0 new messages