469,358 Members | 1,581 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Converting a list to a dictionary

Hi,

is there a short version for this?

res_dict = {}
for resource in res_list:
res_dict[resource.get_id()] = resource

This does not work:

res_dict = dict([r.get_id(), r for r in res_list])

-Samuel

Mar 14 '07 #1
9 2182
Samuel a écrit :
Hi,

is there a short version for this?

res_dict = {}
for resource in res_list:
res_dict[resource.get_id()] = resource

This does not work:

res_dict = dict([r.get_id(), r for r in res_list])
res_dict = dict((r.get_id(), r) for r in res_list)

or if you have to be compatible with older python versions:

res_dict = dict([(r.get_id(), r) for r in res_list])

HTH
Mar 14 '07 #2
Samuel wrote:
This does not work:

res_dict = dict([r.get_id(), r for r in res_list])
This does:

res_dict = dict([(r.get_id(), r) for r in res_list])
Mar 14 '07 #3
On Mar 14, 9:52 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
res_dict = dict((r.get_id(), r) for r in res_list)

or if you have to be compatible with older python versions:

res_dict = dict([(r.get_id(), r) for r in res_list])
Yep, that works. Strange, I was sure I had tested the latter, but I
must have been doing something wrong.

Thanks for your help!

-Samuel

Mar 14 '07 #4
On Mar 14, 4:52 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
res_dict = dict((r.get_id(), r) for r in res_list)
I'm using Python2.5 and it seems that this only gives me a hash with
the first id and first record. Am I doing something wrong?
>>class Person():
.... def __init__(self):
.... self.id = 5
....
>>mylist = []
for i in range(100):
.... mylist.append(Person())
....
>>mydict = dict((r.id,r) for r in mylist)
mydict
{5: <__main__.Person instance at 0x00A99EE0>}
>>>
Mar 14 '07 #5
On Mar 14, 9:32 pm, "Drew" <olso...@gmail.comwrote:
I'm using Python2.5 and it seems that this only gives me a hash with
the first id and first record. Am I doing something wrong?
Try this instead:
>>class Person():
.... def __init__(self):
.... self.id = 5
....
>>mylist = []
for i in range(100):
.... p = Person()
.... p.id = i
.... mylist.append(p)
....
>>mydict = dict((r.id,r) for r in mylist)
mydict
What this does is it maps the id to the object. In your case, you only
have one id.

-Samuel

Mar 14 '07 #6
On Mar 14, 4:43 pm, "Samuel" <knipk...@gmail.comwrote:
What this does is it maps the id to the object. In your case, you only
have one id.

-Samuel
This is interesting behavior, but may not be what the original poster
intended. If I understand correctly, this means that if more than one
object shares the same id, only one copy will be created in the dict.
Is this correct?

Mar 14 '07 #7
On Mar 14, 9:48 pm, "Drew" <olso...@gmail.comwrote:
This is interesting behavior, but may not be what the original poster
intended.
I am the original poster :).
If I understand correctly, this means that if more than one
object shares the same id, only one copy will be created in the dict.
Is this correct?
Yes. Dictionaries are just hashes; you can't have the same key twice.

Bye,
-Sam

Mar 14 '07 #8
Drew wrote:
On Mar 14, 4:52 pm, Bruno Desthuilliers
<bdesth.quelquech...@free.quelquepart.frwrote:
>res_dict = dict((r.get_id(), r) for r in res_list)

I'm using Python2.5 and it seems that this only gives me a hash with
the first id and first record. Am I doing something wrong?
>>>class Person():
... def __init__(self):
... self.id = 5
...
>>>mylist = []
for i in range(100):
... mylist.append(Person())
...
>>>mydict = dict((r.id,r) for r in mylist)
mydict
{5: <__main__.Person instance at 0x00A99EE0>}
Well, you aren't actually using the object's id() value as the dict key,
but the value of its id attribute. Since all the instances you create
have the same value for that attribute each change to the dict (except
the first) overwrites the immediately preceding change - you can't have
100 values in a dict all with the same key!

Try:
>>mydict = dict((id(r), r) for r in mylist)
and you'll find you then get a dict with 100 elements.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007

Mar 14 '07 #9
On Mar 14, 4:52 pm, "Samuel" <knipk...@gmail.comwrote:
On Mar 14, 9:48 pm, "Drew" <olso...@gmail.comwrote:
This is interesting behavior, but may not be what the original poster
intended.

I am the original poster :).
If I understand correctly, this means that if more than one
object shares the same id, only one copy will be created in the dict.
Is this correct?

Yes. Dictionaries are just hashes; you can't have the same key twice.

Bye,
-Sam
Doh! *Hangs head in shame and walks away slowly...* Thanks for you
gracious response :)

Mar 14 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Robert Oschler | last post: by
1 post views Thread by rieh25 | last post: by
20 posts views Thread by Gustaf | last post: by
10 posts views Thread by Frank Rizzo | last post: by
11 posts views Thread by Louis.Soninhu | last post: by
reply views Thread by Gabriel Genellina | last post: by
12 posts views Thread by =?Utf-8?B?THVpZ2k=?= | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.