Converting a list to a dictionary

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Mar 14 16:52:14 EDT 2007


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



More information about the Python-list mailing list