try vs. has_key()
Jeremy Hylton
jeremy at cnri.reston.va.us
Fri Apr 30 11:08:30 EDT 1999
>>>>> "JE" == Jeff Epler <jepler at inetnebr.com> 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
append.
Jeremy
More information about the Python-list
mailing list