Re: Converting a list to a dictionary

2007-03-14 Thread Drew
On Mar 14, 4:52 pm, "Samuel" <[EMAIL PROTECTED]> wrote: > On Mar 14, 9:48 pm, "Drew" <[EMAIL PROTECTED]> wrote: > > > 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 on

Re: Converting a list to a dictionary

2007-03-14 Thread Samuel
On Mar 14, 9:48 pm, "Drew" <[EMAIL PROTECTED]> wrote: > 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 di

Re: Converting a list to a dictionary

2007-03-14 Thread Steve Holden
Drew wrote: > On Mar 14, 4:52 pm, Bruno Desthuilliers > <[EMAIL PROTECTED]> wrote: > >> 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():

Re: Converting a list to a dictionary

2007-03-14 Thread Drew
On Mar 14, 4:43 pm, "Samuel" <[EMAIL PROTECTED]> wrote: > 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 ob

Re: Converting a list to a dictionary

2007-03-14 Thread Samuel
On Mar 14, 9:32 pm, "Drew" <[EMAIL PROTECTED]> wrote: > 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 =

Re: Converting a list to a dictionary

2007-03-14 Thread Drew
On Mar 14, 4:52 pm, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > 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):

Re: Converting a list to a dictionary

2007-03-14 Thread Samuel
On Mar 14, 9:52 pm, Bruno Desthuilliers <[EMAIL PROTECTED]> wrote: > 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 lat

Re: Converting a list to a dictionary

2007-03-14 Thread Leif K-Brooks
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]) -- http://mail.python.org/mailman/listinfo/python-list

Re: Converting a list to a dictionary

2007-03-14 Thread Bruno Desthuilliers
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 ha

Converting a list to a dictionary

2007-03-14 Thread Samuel
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 -- http://mail.python.org/mailman/listinfo/python-list