new_d = {} for key in d: if "." not in key: new_d[key] = d[key] else: lst = key.split(".") l = len(lst) now_d = new_d for i in range(l-1): try: now_d[lst[i]] except: now_d[lst[i]] = {} now_d = now_d[lst[i]] now_d[lst[-1]] = d[key]