try vs. has_key()

Aahz Maruch aahz at netcom.com
Tue Apr 27 19:17:23 EDT 1999


In article <7g51q6$1pt$1 at vvs.superst.iae.nl>,
Carel Fellinger  <cfelling at iae.nl> wrote:
>In article <Pine.SUN.3.95-heb-2.07.990423140345.21577A-100000 at sunset.ma.huji.ac.il> you wrote:
>>
>> d={}
>> for word in words:
>> 	first_two=word[:2]
>> 	d[first_two]=d.get(first_two, []).append(word)
>
>this is the statement where i get lost. and when i try it with python 1.5.1
>it doesn't work either. As far as i understand the append function it doesn't
>return anything as it is just a shorthand for an assignment which to my
>knowledge even in python 1.5.2 doesn't return values. or am i missing
>something here? nonetheless, it looks great, and i sure hope it works too.

It appears that the second parameter for get() (to specify a value
different from None when the key is not found) is new to 1.5.2.  In
1.5.1 you still have to do the following:

d={}
for word in words:
 	first_two=word[:2]
	if d.get(first_two) is None :
 		d[first_two]=[word]
	else :
 		d[first_two].append(word)

(I think, without testing the code)
-- 
                      --- Aahz (@netcom.com)

Hugs and backrubs -- I break Rule 6       <*>      http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het

Hi!  I'm a beta for SigVirus 2000!  Copy me into your .sigfile!




More information about the Python-list mailing list