If we are OK moving away from the in-place implementation, this whole block above could be replaced with a dict comprehension.