From: jeremy at cnri.reston.va.us (Jeremy Hylton) Date: Fri, 30 Apr 1999 11:08:30 -0400 (EDT) Subject: try vs. has_key() In-Reply-To: References: <7g51q6$1pt$1@vvs.superst.iae.nl> <14119.8145.293039.667256@bitdiddle.cnri.reston.va.us> Message-ID: <14121.50708.131339.251436@bitdiddle.cnri.reston.va.us> X-UID: 219 >>>>> "JE" == Jeff Epler 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