Converting to lists into one dictionary

Ivan Van Laningham ivanlan at callware.com
Mon Oct 4 14:59:16 EDT 1999


Hi All--

Skip Montanaro wrote:
> 

[bobbit]

> Sure.  Try:
> 
>     >>> list1=['a','b','c']
>     >>> list2=['aaa','bbb','ccc']
>     >>> dict1={}
>     >>> for k, v in map(None, list1, list2):
>     ...   dict1[k]=v
>     ...
>     >>> print dict1
>     {'b': 'bbb', 'c': 'ccc', 'a': 'aaa'}
> 
> It also does the None pad of list2 you asked about.  It will None pad list1
> as well, resulting in probably not what you want (the last element of list2
> being associated with the key None).
> 

This makes me wonder--why is there no canonical "dict()" built in
function?  We have "long()", "list()" and so on, but nothing like them
for dictionaries.  They all know what to do when fed improper input.

Probably, everyone is going to tell me it's too complex an issue, and
this is of course true to a degree.  Nonetheless, there ought to be a
straightforward enough way to construct a dictionary from a list, at the
very least, that it could be built in.

<list-in-garbage-out>-ly y'rs,
Ivan
----------------------------------------------
Ivan Van Laningham
Callware Technologies, Inc.
ivanlan at callware.com
ivanlan at home.com
http://www.pauahtun.org
See also: 
http://www.foretec.com/python/workshops/1998-11/proceedings.html
Army Signal Corps:  Cu Chi, Class of '70
----------------------------------------------




More information about the Python-list mailing list