Converting to lists into one dictionary

Emile van Sebille emile at fenx.com
Mon Oct 4 21:00:43 EDT 1999


How about:

>>> a = [1,2,3]
>>> b = ['a','b','c']
>>> c = {}
>>> def d(a,b):
 c[a]=b
>>> toss = map(d,a,b)
>>> c
{3: 'c', 2: 'b', 1: 'a'}

--

Emile van Sebille
emile at fenx.com
-------------------


Boudewijn Rempt <boud at rempt.xs4all.nl> wrote in message
news:7tanv8$ga$1 at essle.valdyas.org...
> 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.
>
> --
>
> Boudewijn Rempt  | http://www.xs4all.nl/~bsarempt






More information about the Python-list mailing list