Converting to lists into one dictionary
M.-A. Lemburg
mal at lemburg.com
Sun Oct 10 08:05:37 EDT 1999
Boudewijn Rempt wrote:
>
> I was wondering whether it is possible to convert two lists into one
> dictionary with one simple function: i.e., I've got a list of keys,
> and a list of values like this:
>
> list1=['a','b','c']
> list2=['aaa','bbb','ccc']
>
> and I want do something easy and speedy to these two lists that
> will give me:
>
> dict1={'a': 'aaa', 'b': 'bbb', 'c': 'ccc'}
>
> preferably with the proviso that if there aren't enough values in
> list2, the dictionary is padded out with Nones. Is there something
> built in, or do I have to loop trough list1 myself? I'm going to do
> this an awful lot of times.
mxTools (available via my Python Pages below) has a dict() function
which converts a sequence of tuples to a dictionary. It also has a
function for zipping (or transposing) two lists:
>>> import NewBuiltins # mxTools provides this
>>> list1=['a','b','c']
>>> list2=['aaa','bbb','ccc']
>>> dict(tuples(list1,list2))
{'b': 'bbb', 'c': 'ccc', 'a': 'aaa'}
--
Marc-Andre Lemburg
______________________________________________________________________
Y2000: 82 days left
Business: http://www.lemburg.com/
Python Pages: http://www.lemburg.com/python/
More information about the Python-list
mailing list