469,323 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

List to dict conversion

Hi!

From list:
l ['key1:val1', 'key2:val2']

I want dict: d {'key2': 'val2', 'key1': 'val1'}

Is there any better (faster, simplier, prettier?) solution than this? for line in l:

.... spl=line.split(':')
.... d[spl[0]] = spl[1]
Regards.

Jul 18 '05 #1
5 19181
Wujek wrote:
Hi!

From list:
>>> l ['key1:val1', 'key2:val2']

I want dict: >>> d {'key2': 'val2', 'key1': 'val1'}

Is there any better (faster, simplier, prettier?) solution than this? >>> for line in l:

... spl=line.split(':')
... d[spl[0]] = spl[1]


d = dict([ x.split(':') for x in l ])

should work just the same way as your loop -- perhaps marginally
faster (try both with timeit.py, of course, if it DOES matter...!),
and "simpler" and "prettier" are in the eyes of the beholders.
Alex

Jul 18 '05 #2
<quote name="Wujek" date="1065176228" email="wu***@wujek.com">
Hi!

From list:
l ['key1:val1', 'key2:val2']

I want dict: d {'key2': 'val2', 'key1': 'val1'}

Is there any better (faster, simplier, prettier?) solution than this? for line in l:

... spl=line.split(':')
... d[spl[0]] = spl[1]

</quote>

How about:
dict([s.split(':') for s in l])

Don't know if it's faster, simplier or prettier, but I find it easier to read.
This may not be the case if you aren't familiar with list comprehensions, though.

Gerrit.

--
Mozilla _is_ the web: it grows faster than you can download it.
1011001 1101111 1110101 1110010 1110011 0101100
1000111 1100101 1110010 1110010 1101001 1110100

Jul 18 '05 #3
Gerrit Holl wrote:
How about:
dict([s.split(':') for s in l])

Don't know if it's faster, simplier or prettier, but I find it easier to read.
This may not be the case if you aren't familiar with list comprehensions, though.


No, I like it. Thank you (and Alex too).

Regards.

Jul 18 '05 #4

"Wujek" <wu***@wujek.com> wrote in message
news:bl**********@atlantis.news.tpi.pl...
Hi!

From list:
>>> l ['key1:val1', 'key2:val2']

I want dict: >>> d

{'key2': 'val2', 'key1': 'val1'}


Guessing that this may have been read from a file, you may also be
interested in looking at ConfigParser. From the module:

DESCRIPTION
A setup file consists of sections, lead by a "[section]" header,
and followed by "name: value" entries, with continuations and such in
the style of RFC 822.

--
Emile van Sebille
em***@fenx.com
Jul 18 '05 #5
Emile van Sebille wrote:
Guessing that this may have been read from a file, you may also be
Indeed.
interested in looking at ConfigParser. From the module:


Data will be read from number of small files. ConfigParser is too big
gun for this fly, I think. This oneliner (from previous posts in this
thread) is much better for this task.

Regards.

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by David Bear | last post: by
30 posts views Thread by Steven Bethard | last post: by
90 posts views Thread by Christoph Zwerschke | last post: by
20 posts views Thread by Seongsu Lee | last post: by
27 posts views Thread by Mark | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.