[Tutor] two lists to keys and values in dictionary
Doug.Shawhan@gecits.ge.com
Doug.Shawhan@gecits.ge.com
Mon, 12 Aug 2002 13:48:44 -0400
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.... :-)
Thanks!