def incr_nestedctr(d, *keys, **kwargs): """ >>> a = {} >>> incr_nestedctr(a, 'a', 'b', 'c', 'd') {'a': {'b': {'c': {'d': 1}}}} >>> incr_nestedctr(a, 'a', 'b', 'c', 'd') {'a': {'b': {'c': {'d': 2}}}} >>> incr_nestedctr(a, 'a', 'b', 'c', 'd', delta = -4) {'a': {'b': {'c': {'d': -2}}}} >>> incr_nestedctr({u'1.0': {u'0': 1, '5': 1}}, '1.0', '5', delta = 2) {u'1.0': {u'0': 1, '5': 3}} """ delta = kwargs.get('delta', 1) thed = d for k in keys[:-1]: thed = thed.setdefault(k, {}) thed.setdefault(keys[-1], 0) thed[keys[-1]] += delta return d