Subject: Re: [Tutor] two lists to keys and values in dictionary

Doug.Shawhan@gecits.ge.com Doug.Shawhan@gecits.ge.com
Mon, 12 Aug 2002 14:56:09 -0400


I have never seen zip before! Guess it's time to rearead my copy of "Python
Standard Library".

Any other sorting tips like this one? :-)

d

-------------------------------

Quoth Gregor:

 >>> zip(l1,l2)
[('wicky', 'hip'), ('wacky', 'hop'), ('woo', 'hoo')]
 >>> pairs = zip(l1,l2)
 >>> key,value = pairs[0]
 >>> key,value
('wicky', 'hip')
 >>> d={}
 >>> d[key]=value
 >>> d
{'wicky': 'hip'}
 >>> d={}
 >>> for key,value in pairs:
    d[key]=value

   
 >>> d
{'woo': 'hoo', 'wacky': 'hop', 'wicky': 'hip'}


Or shortly:

 >>> d={}
 >>> for key,value in zip(l1,l2):
    d[key]=value

   
 >>> d
{'woo': 'hoo', 'wacky': 'hop', 'wicky': 'hip'}
 >>>

hth, gl




--__--__--

Message: 18
Date: Mon, 12 Aug 2002 13:22:19 -0500
Subject: Re: [Tutor] two lists to keys and values in dictionary
From: SA <sarmstrong13@mac.com>
To: <Doug.Shawhan@gecits.ge.com>, <tutor@python.org>

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




--__--__--

Message: 19
Date: Mon, 12 Aug 2002 13:28:42 -0500
Subject: Re: [Tutor] two lists to keys and values in dictionary
From: SA <sarmstrong13@mac.com>
To: SA <sarmstrong13@mac.com>, <Doug.Shawhan@gecits.ge.com>,
   <tutor@python.org>

On 8/12/02 1:22 PM, "SA" <sarmstrong13@mac.com> wrote:

>> 
> 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'}
> 
> 
> 


My bad. I just noticed that the output did not iterate properly over l2.

Sorry.


SA

-- 
"I can do everything on my Mac I used to on my PC. Plus a lot more ..."
-Me




--__--__--

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


End of Tutor Digest