This post shows up in the same day I was looking exactly at it. Quite interesting.
Another way of avoiding KeyError is using
dict.get(val, default_val)
I find it a bit cleaner, since you don't have to create a function or import collection.[1] https://docs.python.org/3/library/stdtypes.html?highlight=di...