34 lines
1.1 KiB
Plaintext
34 lines
1.1 KiB
Plaintext
From: jeremy at cnri.reston.va.us (Jeremy Hylton)
|
|
Date: Thu, 29 Apr 1999 10:44:06 -0400 (EDT)
|
|
Subject: try vs. has_key()
|
|
In-Reply-To: <372789d8.27766891@news.jpl.nasa.gov>
|
|
References: <aahzFAM4oJ.M7M@netcom.com>
|
|
<yWOT2.6007$8m5.9320@newsr1.twcny.rr.com>
|
|
<Pine.SUN.3.95-heb-2.07.990423140345.21577A-100000@sunset.ma.huji.ac.il>
|
|
<37247ea3.494305@news.jpl.nasa.gov>
|
|
<372789d8.27766891@news.jpl.nasa.gov>
|
|
Message-ID: <14120.28625.776623.556213@bitdiddle.cnri.reston.va.us>
|
|
X-UID: 448
|
|
|
|
>>>>> "WD" == William H Duquette <William.H.Duquette at jpl.nasa.gov> writes:
|
|
|
|
WD> As someone pointed out, append() returns None, explaining the
|
|
WD> result I got. However, the following code works just fine:
|
|
d = {}
|
|
a = 'foo'
|
|
|
|
# Append an entry to d[a], whether it has been initialized or not:
|
|
d[a] = d.get(a, [])
|
|
d[a].append('Bar')
|
|
|
|
However, you're doing two dictionary lookups when only one is
|
|
necessary. It would be more efficient to store the results of the get
|
|
in a temporary variable -- temp = d[a] = d.get(a, []) -- and use the
|
|
temporary variable for the append.
|
|
|
|
Jeremy
|
|
|
|
|
|
|
|
|