Just wondering, is it better performance-wise to explicitly check for key membership than to rely on exceptions?
Existing:
for line in sys.stdin:
name, marks = line.rstrip().split('\t')
try:
if agregatedmarks.get(name):
agregatedmarks.get(name).append(marks)
else:
agregatedmarks[name] = [marks]
except ValueError:
pass
Or
for line in sys.stdin:
name, marks = line.rstrip().split('\t')
if name in aggregatedmarks:
aggregatedmarks.append(marks)
else:
aggregatedmarks[name] = [marks]
This is a common idiom I find myself using (append to an existing list or creating a new one in a larger dict).
3 comments
[ 51.6 ms ] story [ 421 ms ] threadJust wondering, is it better performance-wise to explicitly check for key membership than to rely on exceptions?
Existing:
Or This is a common idiom I find myself using (append to an existing list or creating a new one in a larger dict).See http://docs.python.org/release/2.5.2/lib/typesmapping.html