0

I'd like to merge a list of multiple dictionaries using the common key 'Pontos':

all_dicts = [{'Pontos': {'70': {0: {'atleta_id': 92981, 'apelido': 'Ronaldo'}, 
                                1: {'atleta_id': 107677, 'apelido': 'Juninho'}, 
                                2: {'atleta_id': 71937, 'apelido': 'Juninho'} 
                               }
                        }
             }, 
             {'Pontos': {'75': {0: {'atleta_id': 85425, 'apelido': 'João Paulo'}, 
                                1: {'atleta_id': 71937, 'apelido': 'Juninho'}, 
                                2: {'atleta_id': 105998, 'apelido': 'Vinícius'} 
                               }
                        }    
             },...]

Ending up with:

{'Pontos': {'70': {0: {'atleta_id': 92981, 'apelido': 'Ronaldo'}, 
                   1: {'atleta_id': 107677, 'apelido': 'Juninho'}, 
                   2: {'atleta_id': 71937, 'apelido': 'Juninho'} 
                  }
            },
  
             {'75': {0: {'atleta_id': 85425, 'apelido': 'João Paulo'}, 
                     1: {'atleta_id': 71937, 'apelido': 'Juninho'}, 
                     2: {'atleta_id': 105998, 'apelido': 'Vinícius'} 
                    }
            }, ...
}

How do I do this?

8-Bit Borges
  • 9,643
  • 29
  • 101
  • 198

3 Answers3

0

From Aaron Hall's answer:

from copy import deepcopy

def dict_of_dicts_merge(x, y):
    z = {}
    overlapping_keys = x.keys() & y.keys()
    for key in overlapping_keys:
        z[key] = dict_of_dicts_merge(x[key], y[key])
    for key in x.keys() - overlapping_keys:
        z[key] = deepcopy(x[key])
    for key in y.keys() - overlapping_keys:
        z[key] = deepcopy(y[key])
    return z

result = {}
for dict_elem in all_dicts:
   dict_of_dicts_merge(result, dict_elem)

return result
TheoretiCAL
  • 19,461
  • 8
  • 43
  • 65
0

Try this

d={}
for k in all_dicts[0].keys():
 d[k]=tuple(d[k] for d in all_dicts)
Adrien_CH
  • 23
  • 6
0

Using list comprehension and update

def merge_dicts(lst_dicts, keyword):
    # List comprehension to get list of dicts with keyword
    lst = [d[keyword] for d in lst_dicts if d.get(keyword)]

    # Use update to place into one dictionary
    result = {keyword:{}}
    for d in lst:
        result[keyword].update(d)
        
    return result

Usage

result = merge_dicts(all_dicts, 'Pontos')
    

Example

all_dicts = [{'Pontos': {'70': {0: {'atleta_id': 92981, 'apelido': 'Ronaldo'}, 
                                1: {'atleta_id': 107677, 'apelido': 'Juninho'}, 
                                2: {'atleta_id': 71937, 'apelido': 'Juninho'} 
                               }
                        }
             }, 
             {'Pontos': {'75': {0: {'atleta_id': 85425, 'apelido': 'João Paulo'}, 
                                1: {'atleta_id': 71937, 'apelido': 'Juninho'}, 
                                2: {'atleta_id': 105998, 'apelido': 'Vinícius'} 
                               }
                        }    
             },
            {'Other': {'72': {0: {'atleta_id': 12981, 'apelido': 'Ronaldo'}, 
                         1: {'atleta_id': 207677, 'apelido': 'Juninho'}, 
                         2: {'atleta_id': 31937, 'apelido': 'Juninho'} 
                        }
                  }
                
            }]

Result

{'Pontos': {'70': {0: {'atleta_id': 92981, 'apelido': 'Ronaldo'},
   1: {'atleta_id': 107677, 'apelido': 'Juninho'},
   2: {'atleta_id': 71937, 'apelido': 'Juninho'}},
  '75': {0: {'atleta_id': 85425, 'apelido': 'João Paulo'},
   1: {'atleta_id': 71937, 'apelido': 'Juninho'},
   2: {'atleta_id': 105998, 'apelido': 'Vinícius'}}}}
DarrylG
  • 16,732
  • 2
  • 17
  • 23