2
data = {
    "key1": "value1",
    "key2": [
        "value 2.1",
        "value 2.2"
    ],
    "key3": [{
        "key31":"value 3.1",
        "key32":"value 3.2",
        "key33":"value 3.3"
    }]
}

If I have the above dict, how can I update a particular value based on a supplied path? For example, I can easily do data["key3"][0]["key33"] = newValue, but what if I don't know the exact path before hand? Is there a way to create a generic method that will take any number of keys and achieve the same thing?

The best I can do is create a method that takes an input string/path such as "key3.0.key33", split based on the ., store them as string and int types, then only access the value. I cannot think of a way to change that value

DraegerMTN
  • 1,001
  • 2
  • 12
  • 21

0 Answers0