-2

I have an array ['a', 'b', 'c'] , and I want to create a nested object like below.

{ a: {
       b : {
          c: {}
        }
}
Khadija M
  • 54
  • 6
  • Please provide what you have tried and how it went wrong – Chris Mar 05 '21 at 08:19
  • This has already been answered here https://stackoverflow.com/questions/40401886/how-to-create-a-nested-dictionary-from-a-list-in-python – Khadija M Mar 05 '21 at 08:30
  • Does this answer your question? [How to create a nested dictionary from a list in Python?](https://stackoverflow.com/questions/40401886/how-to-create-a-nested-dictionary-from-a-list-in-python) – tevemadar Mar 05 '21 at 08:33
  • That's not an [`array`](https://docs.python.org/3/library/array.html) but a [`list`](https://docs.python.org/3/library/stdtypes.html#typesseq-list). – Matthias Mar 05 '21 at 08:46

1 Answers1

1

How about like this? I implement it as Dictionary

base = ["a", "b", "c"]

result = {}
for b in base[::-1]:
    result = {b: result}

print(result)
# {'a': {'b': {'c': {}}}}
Daisuke Akagawa
  • 484
  • 2
  • 9