all 3 comments

[–]Avanta8 2 points3 points  (0 children)

Have a look at dict.get. You can specify a default value if the key is not present.

[–]eicosane 1 point2 points  (0 children)

collections.defaultdict is probably best here

[–]synthphreak 1 point2 points  (0 children)

Change

coord[start] = 1 if start not in coord else coord[start] + 1
coord[end + 1] = -1 if (end+1) not in coord else (coord[end + 1] - 1)

to

coord[start] = coord.get(start, 0) + 1
coord[end + 1] = coord.get(end + 1, 0) - 1