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).
Just 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).