Converting list to nested dictionary

后端 未结 4 1211
花落未央
花落未央 2021-01-18 08:50

How can I convert a list into nested `dictionary\'?

For example:

l = [1, 2, 3, 4] 

I\'d like to convert it to a dictio

4条回答
  •  难免孤独
    2021-01-18 09:24

    You can do something like this:

    l = [1,2,3,4]
    d = {}
    
    for i in l[::-1]:
        d = {i: d}
    
    print(d)
    

    {1: {2: {3: {4: {}}}}} [Finished in 0.4s]

提交回复
热议问题