[Tutor] Re: create dict from 2 lists
Andrei
project5 at redrival.net
Fri Apr 2 11:14:40 EST 2004
Gregor Lingl wrote on Fri, 02 Apr 2004 15:38:31 +0200:
> BRINER Cedric schrieb:
>
>> 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]
>>
<snip>
> >>> p={}
> >>> for key,value in zip(keys,values):
> p[key]=value
>
Or, even shorter:
>>> dict(zip(keys, values))
{1: 11, 2: 22, 3: 33, 4: 44}
--
Yours,
Andrei
=====
Real contact info (decode with rot13):
cebwrpg5 at jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
More information about the Tutor
mailing list