I wouldn't make any assumptions as to the insertion order of the keys, assuming you want to update the dictionary in place.
dict1 = {8 : {'objects_1' : [1289,12678]} , 6 : {'objects_2' : [7798,808]} , 1 : {'object_2' : [76789,879]} }
old_keys = list(dict1.keys())
for idx in range(1, len(old_keys) + 1):
if idx in old_keys:
old_keys.remove(idx)
else:
old_key = old_keys.pop(0)
dict1[idx] = dict1.pop(old_key)
print(dict1)
Prints:
{1: {'object_2': [76789, 879]}, 2: {'objects_1': [1289, 12678]}, 3: {'objects_2': [7798, 808]}}
Granted this could change the insertion order of the values but will handle any arbitrary input.
If you are running Python 3.7 (or CPython 3.6) or later where the assumption is that a set
maintains insertion order and its pop
method will remove items in insertion order, then you could use:
dict1 = {0 : {'objects_1' : [1289,12678]} , 1 : {'objects_2' : [7798,808]} , 2 : {'object_2' : [76789,879]} }
old_keys = set(dict1.keys())
for idx in range(1, len(old_keys) + 1):
if idx in old_keys:
old_keys.remove(idx)
else:
old_key = old_keys.pop()
dict1[idx] = dict1.pop(old_key)
print(dict1)