You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
1.3 KiB

From: jeremy at (Jeremy Hylton)
Date: Fri, 30 Apr 1999 11:08:30 -0400 (EDT)
Subject: try vs. has_key()
In-Reply-To: <>
References: <>
Message-ID: <>
X-UID: 219
>>>>> "JE" == Jeff Epler <jepler at> writes:
>>The following code would be correct:
>> d={}
>> for word in words:
>> first_two = word[:2]
>> d[first_two]= temp = d.get(first_two, [])
>> temp.append(word)
JE> what about d[first_two] = d.get(first_two, [])+[word] ? Or is
JE> list construction and addition going to be enough more expensive
JE> than the function call to make this a lose as well?
Yeah. Concatenating two lists results in a new list being created
every time (and you're already creating a new list containing the
value of word). Two list allocs is definitely more expensive that an