how to convert list of dict to dict

后端 未结 8 820
执笔经年
执笔经年 2020-12-12 19:00

How to convert list of dict to dict. Below is the list of dict

data = [{\'name\': \'John Doe\', \'age\': 37, \'sex\': \'M\'},
        {\'name\': \'Lisa Simp         


        
8条回答
  •  天命终不由人
    2020-12-12 19:33

    Just in case you wanted a functional alternative (also assuming the names are wanted as the new keys), you could do

    from toolz.curried import *
    
    data = [{'name': 'John Doe', 'age': 37, 'sex': 'M'},
            {'name': 'Lisa Simpson', 'age': 17, 'sex': 'F'},
            {'name': 'Bill Clinton', 'age': 57, 'sex': 'M'}]
    
    newdata = pipe(data, 
                   map(lambda x: {x['name']: dissoc(x, 'name')}),
                   lambda x: merge(*x)
    )
    
    print(newdata)
    

提交回复
热议问题