473,395 Members | 1,678 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,395 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 19306
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 called 'alist'. Is there an easy way to create a...
30
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course,...
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 possible and desirable to change the old behavior in...
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 expects a List<string>. I often do the following: ...
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 should be constant time, but does anyone have a...
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 5 keys. dict = {1: , 2: , 900000: , 900001:...
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 2 3 2 1
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, field_id, field_name, value sql_result=, , ,
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.