-1

I have following dict:

["sector", "food", "delivery"]

How can I create with this list following dict:

    "sector": {
        "food": {
            "delivery": {}
        }
    }
sg_sg94
  • 2,248
  • 4
  • 28
  • 56
  • Did you try writing code to solve the problem? What difficulty did you encounter while doing so? Please read https://meta.stackoverflow.com/questions/261592. – Karl Knechtel Jan 31 '22 at 21:31

1 Answers1

0

Iterate over the list and add a dictionary for each key:

>>> n = nested = {}
>>> for k in ["sector", "food", "delivery"]:
...     n = n.setdefault(k, {})
...
>>> nested
{'sector': {'food': {'delivery': {}}}}
Samwise
  • 68,105
  • 3
  • 30
  • 44