[Tutor] create dict from 2 lists
Karl Pflästerer
sigurd at 12move.de
Fri Apr 2 10:50:37 EST 2004
On 2 Apr 2004, Gregor Lingl <- glingl at aon.at wrote:
> BRINER Cedric schrieb:
>> Is the simpliest way to do this, is to do :
>> p={}
>> for i in range(len(keys)):
>> p[keys[i]]=values[i]
>> or is there a hidded function which do this?
> You could use the non-hidden function zip:
> (See: Python Library Reference 2.1)
> >>> keys=[1,2,3,4]
> >>> values=[11,22,33,44]
> >>> p={}
> >>> for key,value in zip(keys,values):
> p[key]=value
> >>> p
> {1: 11, 2: 22, 3: 33, 4: 44}
> Regards, Gregor
You don't even need a loop for that.
>>> keys=[1,2,3,4]
>>> values=[11,22,33,44]
>>> dict(zip(keys, values))
{1: 11, 2: 22, 3: 33, 4: 44}
Karl
--
Please do *not* send copies of replies to me.
I read the list
More information about the Tutor
mailing list