I have a piece of code which is iterating a list and then accumulate the values in a dict based on the keys, the ugly part is that I need to check if the key already exist in the dict, if not then I need to set the value to zero first.
I'm wondering if there's more elegant style to implement this:
for r in records:
if not d.has_key(r[0]):
d[r[0]] = 0
d[r[0]] += r[1]
And further more, if the value is a list, then how to rewrite the following code:
for (key, value) in records:
if not d.has_key(key):
d[key] = []
d[key].append(value)