472,353 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 19259
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: David Bear | last post by:
I know there must be a better way to phrase this so google understands, but I don't know how.. So I'll ask people. Assume I have a list object...
30
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> ...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be...
6
by: buzzweetman | last post by:
Many times I have a Dictionary<string, SomeTypeand need to get the list of keys out of it as a List<string>, to pass to a another method that...
4
by: Emin | last post by:
Dear Experts, How much slower is dict indexing vs. list indexing (or indexing into a numpy array)? I realize that looking up a value in a dict...
6
by: psbasha | last post by:
Hi, I am assigning the list/dict values to another list/dict variable as shown below. List : List1 = List2 = List1
20
by: Seongsu Lee | last post by:
Hi, I have a dictionary with million keys. Each value in the dictionary has a list with up to thousand integers. Follow is a simple example with...
27
by: Mark | last post by:
Hi all, I have a scenario where I have a list like this: User Score 1 0 1 1 1 5...
3
by: rewonka | last post by:
Hello, I'm trying to find the fastest way to convert an sql result into a dict or list. What i mean, for example: my sql result: contact_id,...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.