473,394 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Faster (smarter?) dictionary building

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

Jul 18 '05 #1
2 1490
In article <ma************************************@python.org >,
"Michael T. Babcock" <mb******@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
Jul 18 '05 #2
On Thu, 30 Oct 2003 14:45:48 -0500, "Michael T. Babcock" <mb******@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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: kbass | last post by:
I am new to Python and I am attempting to retrieve data from a database and I would like to place this data into a nested dictionary. After placing the data into a dictionary, I would like to loop...
6
by: Kamilche | last post by:
I have a routine that I really need, but it slows down processing significantly. Can you spot any ineffeciencies in the code? This code makes a critical function of mine run about 7x slower than...
37
by: seberino | last post by:
I've been reading the beloved Paul Graham's "Hackers and Painters". He claims he developed a web app at light speed using Lisp and lots of macros. It got me curious if Lisp is inherently faster...
12
by: rudysanford | last post by:
I just started messing with programming and started with Python. Part of my first project deals with translating numerical values to letters. I would like to be able to do the reverse as well,...
3
by: Rich Shepard | last post by:
I need to learn how to process a byte stream from a form reader where each pair of bytes has meaning according to lookup dictionaries, then use the values to build an array of rows inserted into a...
2
by: Venkat | last post by:
Hi, I would like to have a multiple key dictionary to find a value based on the given key. Here is the example to demonstrate what I mean.
7
by: Andrew Robinson | last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v> depending on input parameters and options to the method. 1. Is there any way to convert from a dictionary to a list...
25
by: John Nagle | last post by:
Some faster Python implementations are under development. JPython has been around for a while, and PyPy and ShedSkin continue to move forward. It's worth thinking about what slows down Python...
10
by: mk | last post by:
Calvin Spealman wrote: Well, basically nothing except I need to remember I have to do that. Suppose one does that frequently in a program. It becomes tedious. I think I will define some helper...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.