Making a dict from two lists/tuples

Andreas Jung andreas at andreas-jung.com
Thu May 24 06:51:41 EDT 2001


dict={}

for i in range(len(keys)):
    dict[keys[i]] = values[i]


Cheers,
Andreas
"Andrew Stribblehill" <a.d.stribblehill at durham.ac.uk> schrieb im Newsbeitrag
news:87u22bw197.fsf at womble.dur.ac.uk...
>
> I want a neat way to turn
>
> keys = ('foo', 'bar')
> values = (1, 2)
>
> into
>
> dict = {'foo': 1, 'bar': 2}.
>
>
> I'm sure there's some (possibly-functional) idiom to do this in one
> simple line, but I'm totally unable to work out what!
>
> I would append my present solution but it's embarassing.
>
> --
> Andrew Stribblehill
> Systems programmer, IT Service, University of Durham, England
> --
> http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list