Convert list of dictionaries to nested dictionary

后端 未结 2 1181
梦毁少年i
梦毁少年i 2021-02-10 12:05

There are a number of questions on this topic but I have not yet been able to adapt solutions to fit my case. Supposed I have a list of dictionaries that I got from a flat file:

2条回答
  •  天涯浪人
    2021-02-10 12:46

    Use a defaultdict for ease of processing these entries:

    output = defaultdict(dict)
    
    for person in people:
        output[person['Name']][person['Attribute']] = person['Value']
    

提交回复
热议问题