Python - Flatten the list of dictionaries

后端 未结 4 1338
情话喂你
情话喂你 2021-01-01 09:54

List of dictionaries:

data = [{
         \'a\':{\'l\':\'Apple\',
                \'b\':\'Milk\',
                \'d\':\'Meatball\'},
         \'b\':{\'favou         


        
4条回答
  •  别那么骄傲
    2021-01-01 10:44

    If you have nested dictionaries with only 'a' and 'b' keys, then I suggest the following solution I find fast and very easy to understand (for readability purpose):

    L = [x['a'] for x in data]
    b = [x['b'] for x in data]
    
    for i in range(len(L)):
        L[i].update(b[i])
    
    # timeit ~1.4
    
    print(L)
    

提交回复
热议问题