Convert nested dictionary into a dictionary

后端 未结 2 1013
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-21 10:00

I have a list of dictionary like this

[
 {\'id\':1, \'name\': \'name1\', \'education\':{\'university\':\'university1\', \'subject\': \'abc1\'}},
 {\'id\':2, \'na         


        
2条回答
  •  别那么骄傲
    2021-01-21 10:25

    You could simply do the following:

    l = [...]
    
    for d in l:
       d.update(d.pop('education', {}))
    
    # l
    [{'id': 1, 'name': 'name1', 'subject': 'abc1', 'university': 'university1'},
     {'id': 2, 'name': 'name2', 'subject': 'abc2', 'university': 'university2'},
     {'id': 3, 'name': 'name3', 'subject': 'abc3', 'university': 'university3'}] 
    

提交回复
热议问题