Convert list of dictionaries to nested dictionary

后端 未结 2 1428
天涯浪人
天涯浪人 2021-02-10 12:22

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:40

    Use a defaultdict for ease of processing these entries:

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

提交回复
热议问题