[Tutor] create dict from 2 lists
Gregor Lingl
glingl at aon.at
Fri Apr 2 08:38:31 EST 2004
BRINER Cedric schrieb:
> hi,
>
> assume that I want to create the dict:
> p={1:11,2:22,3:33,4:44}
>
> and that i have:
> the keys=[1,2,3,4] and
> the values=[11,22,33,44]
>
> 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
> Cédric BRINER
>
More information about the Tutor
mailing list