[Tutor] two lists to keys and values in dictionary
SA
sarmstrong13@mac.com
Mon, 12 Aug 2002 13:22:19 -0500
On 8/12/02 12:48 PM, "Doug.Shawhan@gecits.ge.com"
<Doug.Shawhan@gecits.ge.com> wrote:
> Okay, I have two lists:
>
> l1=['wicky', 'wacky', 'woo']
> l2=['hip', 'hop', 'hoo']
>
> and a dictionary
>
> d={}
>
> I want to create entries in the dictionary with the contents of l1 as keys
> and l2 as values. I can do it this way:
>
>>>> count = 0
>>>> for each in l1:
> d[each]= l2[count]
> count = count + 1
>
>
>>>> d
> {'woo': 'hoo', 'wacky': 'hop', 'wicky': 'hip'}
>
> Since both lists have equal values, is there a way to combine both additions
> to the dictionary in the same loop without the kludgey counter?
>
> Something along the lines of:
>
> for k, v in l1, l2:
> d[k]=v
>
> It seems I have seen something similar done, but since I dont' have my books
> today and lunch is short.... :-)
>
Here is another bit of a cludgeon:
>>> d={}
>>> for k in l1:
... for v in l2:
... d[k]=v
...
>>> print d
{'woo': 'hoo', 'wacky': 'hoo', 'wicky': 'hoo'}
Hope this helps. (one good turn deserves another;))
SA
--
"I can do everything on my Mac I used to on my PC. Plus a lot more ..."
-Me