I have a dictionary, loaded from JSON, like so:
data = {
"1": [
"data",
"data"
],
"2": [
"data",
"data"
],
"3": [
"data",
"data"
]
"5": [
"data",
"data"
]
}
In this instance, "4" is missing, since it has been deleted through another function.
I am trying to write a function that will re-organize/sort the dictionary. This involves fixing holes such as this. In this instance, the numbers should go from 1 to 4 with no holes, this means changing the key-name 5 to 4.
I have written some code to find out which number is missing:
nums = [1, 2, 3, 5]
missing= 0
for x in range(len(data)):
if x not in nums and x is not 0:
missing += x
Greatly appreciate some help. I am simply stuck on how to proceed.
PS: I realize it may not be an optimal data structure. It is like this so I can easily match integers given as system arguments to keys and thus finding corresponding values.
So I just figured out a very easy way of doing it... was a lot simpler than I thought it would be.
for x in range(len(data)):
for k, v in data.items():
data[x] = data.pop(k)
break