Making a dict from two lists/tuples

Remco Gerlich scarblac at pino.selwerd.nl
Thu May 24 17:16:09 CEST 2001


Jonas Meyer Rasmussen <meyer at kampsax.dtu.dk> wrote in comp.lang.python:
> for key,value in keys,values
>     dict[key] = value

Makes the dictionary {keys: values}. Not too useful. Use zip(), ie

dict = {}
for key, value in zip(keys, values):
   dict[key] = value
   
Or in old versions, map(None, ...):

dict = {}
for key, value in map(None, keys, values):
   dict[key] = value
   
These behave in different ways if keys and values aren't of equal length,
but that shouldn't happen anyway.

-- 
Remco Gerlich



More information about the Python-list mailing list